Arbeiten mit kategorialen Daten in Python
Kasey Jones
Research Data Scientist
adult = pd.read_csv("data/adult.csv")
adult1 = adult[adult["Above/Below 50k"] == " <=50K"]
adult2 = adult[adult["Above/Below 50k"] == " >50K"]
wird durch {{2}} ersetzt
groupby_object = adult.groupby(by=["Above/Below 50k"])
groupby_object = adult.groupby(by=["Above/Below 50k"])
Funktion anwenden:
groupby_object.mean()
Age fnlgwt Education Num Capital Gain ...
Above/Below 50k
<=50K 36.783738 190340.86517 9.595065 148.752468 ...
>50K 44.249841 188005.00000 11.611657 4006.142456 ...
Einzeiler:
adult.groupby(by=["Above/Below 50k"]).mean()
Option 1: .sum() nur auf zwei Spalten.
adult.groupby(by=["Above/Below 50k"])['Age', 'Education Num'].sum()
Age Education Num
Above/Below 50k
<=50K 909294 237190
>50K 346963 91047
Option 2: .sum() auf alle numerischen Spalten, dann subsetten.
adult.groupby(by=["Above/Below 50k"]).sum()[['Age', 'Education Num']]
Option 1 ist besser – besonders bei großen Datensätzen
adult.groupby(by=["Above/Below 50k", "Marital Status"]).size()
Above/Below 50k Marital Status
<=50K Divorced 3980
Married-AF-spouse 13
Married-civ-spouse 8284
Married-spouse-absent 384
Never-married 10192
Separated 959
Widowed 908
>50K Divorced 463
Married-AF-spouse 10 <--- Nur 10 Einträge
Married-civ-spouse 6692
...
Arbeiten mit kategorialen Daten in Python