如何过滤熊猫Groupby多级?
示例:
import pandas as pd
df = pd.DataFrame( {"Country" : ["Brazil", "Brazil", "Brazil", "Italy", "Italy", "Brazil"],
"City" : ["Sao Paulo", "Sao Paulo", "Rio de Janeiro", "Roma", "Roma", "Sao Paulo"],
"Vehicule" : ["Bike", "Car", "Car", "Car", "Bike", "Bike"],
"Value" : [1, 2, 3, 4, 5,6]})
group = df.groupby(by=["Country", "City", "Vehicule"]).sum().loc[:,"Value"]
print(group)
如何仅通过车辆“ Bike”过滤级别,并显示自行车和汽车的百分比。
答案 0 :(得分:0)
一种简单的方法是先过滤,然后再分组:
df[df.Vehicule=="Bike"].groupby(["Country", "City"]).sum()
或者:
df.groupby(["Country", "City", "Vehicule"]).sum().unstack().Value.Bike
后者为没有城市的城市产生NaN:
Country City
Brazil Rio de Janeiro NaN
Sao Paulo 7.0
Italy Roma 5.0
或作为分数:
x = df.groupby(["Country", "City", "Vehicule"]).sum().unstack().Value
x.Bike / x.sum(1)
给你:
Country City
Brazil Rio de Janeiro NaN
Sao Paulo 0.777778
Italy Roma 0.555556