pandas'ta kategorik veriler

Python'da Kategorik Verilerle Çalışma

Kasey Jones

Research Data Scientist

dtypes: object

adult = pd.read_csv("data/adult.csv")
adult.dtypes
Age                 int64
Workclass          object
fnlgwt              int64
Education          object
Education Num       int64
Marital Status     object
Occupation         object
Relationship       object
...
Python'da Kategorik Verilerle Çalışma

dtypes: categorical

Varsayılan dtype:

adult["Marital Status"].dtype
dtype('O')

Kategorik olarak ayarlayın:

adult["Marital Status"] = adult["Marital Status"].astype("category")
adult["Marital Status"].dtype
CategoricalDtype(categories=[' Divorced', ' Married-AF-spouse',
' Married-civ-spouse', ' Married-spouse-absent', ' Never-married',
' Separated', ' Widowed'], ordered=False)
Python'da Kategorik Verilerle Çalışma

Kategorik Series oluşturma

my_data = ["A", "A", "C", "B", "C", "A"]
my_series1 = pd.Series(my_data, dtype="category")
print(my_series1)
0    A
1    A
2    C
...
dtype: category
Categories (3, object): [A, B, C]
Python'da Kategorik Verilerle Çalışma

Kategorik Series oluşturma

my_data = ["A", "A", "C", "B", "C", "A"]
my_series2 = pd.Categorical(my_data, categories=["C", "B", "A"], ordered=True)
my_series2
[A, A, C, B, C, A]
Categories (3, object): [C < B < A]
Python'da Kategorik Verilerle Çalışma

Neden kategorik kullanırız: bellek

Bellek tasarrufu:

adult = pd.read_csv("data/adult.csv")
adult["Marital Status"].nbytes
260488
adult["Marital Status"] = adult["Marital Status"].astype("category")
adult["Marital Status"].nbytes
32617
Python'da Kategorik Verilerle Çalışma

Veri okurken dtype belirtme

  1. Bir sözlük oluşturun:

    adult_dtypes = {"Marital Status": "category"}
    
  2. dtype parametresini ayarlayın:

    adult = pd.read_csv("data/adult.csv", dtype=adult_dtypes)
    
  3. dtype değerini kontrol edin:

    adult["Marital Status"].dtype
    
CategoricalDtype(categories=[' Divorced', ' Married-AF-spouse',
                             ..., ' Widowed'], ordered=False)
Python'da Kategorik Verilerle Çalışma

pandas kategori alıştırması

Python'da Kategorik Verilerle Çalışma

Preparing Video For Download...