Werken met categorische data 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"]
wordt vervangen door
groupby_object = adult.groupby(by=["Above/Below 50k"])
groupby_object = adult.groupby(by=["Above/Below 50k"])
Pas een functie toe:
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 ...
One-liner:
adult.groupby(by=["Above/Below 50k"]).mean()
Optie 1: voert alleen .sum() uit op twee kolommen.
adult.groupby(by=["Above/Below 50k"])['Age', 'Education Num'].sum()
Age Education Num
Above/Below 50k
<=50K 909294 237190
>50K 346963 91047
Optie 2: voert .sum() uit op alle numerieke kolommen en subset daarna.
adult.groupby(by=["Above/Below 50k"]).sum()[['Age', 'Education Num']]
Optie 1 heeft de voorkeur — vooral bij grote datasets
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 <--- Slechts 10 records
Married-civ-spouse 6692
...
Werken met categorische data in Python