Kategorik tuzaklar

Python'da Kategorik Verilerle Çalışma

Kasey Jones

Research Data Scientist

İkinci el arabalar: son veri kümesi

import pandas as pd

used_cars = pd.read_csv("used_cars.csv")
used_cars.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 38531 entries, 0 to 38530
Data columns (total 30 columns):
 #   Column             Non-Null Count  Dtype  
 --  ------             --------------  -----  
 0   manufacturer_name  38531 non-null  object 
 1   model_name         38531 non-null  object 
 2   transmission       38531 non-null  object  
...
Python'da Kategorik Verilerle Çalışma

Büyük bellek tasarrufu

used_cars['manufacturer_name'].describe()
count          38531
unique            55
top       Volkswagen
freq            4243
Name: manufacturer_name, dtype: object
print("As object: ", used_cars['manufacturer_name'].nbytes)
print("As category: ", used_cars['manufacturer_name'].astype('category').nbytes)
As object: 308248
As category: 38971
1 https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html
Python'da Kategorik Verilerle Çalışma

Az bellek tasarrufu

used_cars['odometer_value'].astype('object').describe()
count      38531
unique      6063
top       300000
freq        1794
Name: odometer_value, dtype: int64
print(f"As float: {used_cars['odometer_value'].nbytes}")
print(f"As category: {used_cars['odometer_value'].astype('category').nbytes}")
As float: 308248
As category: 125566
Python'da Kategorik Verilerle Çalışma

Kategorilerle çalışmak zor olabilir

  • Veriyi değiştirmek için .str erişimini kullanmak, Seriyi object’e çevirir.
  • .apply() yöntemi, object türünde yeni bir Seri döndürür.
  • Kategori ekleme, kaldırma, değiştirme veya ayarlama yöntemleri, eksik kategorileri aynı şekilde işlemez.
  • NumPy işlevleri genelde kategorik Serilerle çalışmaz.
Python'da Kategorik Verilerle Çalışma

Kontrol ve dönüştürme

Kontrol edin

used_cars["color"] = used_cars["color"].astype("category")
used_cars["color"] = used_cars["color"].str.upper()
print(used_cars["color"].dtype)
object

Dönüştürün

used_cars["color"] = used_cars["color"].astype("category")
print(used_cars["color"].dtype)
category
Python'da Kategorik Verilerle Çalışma

Eksik değerlere bakın

Kategorileri ayarlayın

used_cars["color"] = used_cars["color"].astype("category")
used_cars["color"].cat.set_categories(["black", "silver", "blue"], inplace=True)

used_cars["color"].value_counts(dropna=False)
NaN       18172
black      7705
silver     6852
blue       5802
Name: color, dtype: int64
Python'da Kategorik Verilerle Çalışma

NumPy dizilerini kullanma

used_cars['number_of_photos'] = used_cars['number_of_photos'].astype("category")
used_cars['number_of_photos'].sum()  # <--- Hata verir
TypeError: Categorical cannot perform the operation sum
used_cars['number_of_photos'].astype(int).sum()

Not:

# .str sütunu bir diziye dönüştürür
used_cars["color"].str.contains("red")
0        False
1        False
...
Python'da Kategorik Verilerle Çalışma

Tuzak alıştırması

Python'da Kategorik Verilerle Çalışma

Preparing Video For Download...