我想计算每个社区的破水点百分比。到目前为止,我能够获得社区名单和破水点。
到目前为止,这是我的代码:
import pandas as pd
df = pd.DataFrame((data))
gb = df.groupby(['water_point_condition'])
grouped = gb[["communities_villages", "water_point_condition"]].get_group("broken")
print(grouped)
结果是:
这解决了我的问题,我能够获得每个社区的破水点百分比:
df = pd.DataFrame(data)
grouped = df.groupby(['water_point_condition'])
rank_by_percentage = 100 * df[df.water_point_condition == 'broken'].communities_villages.value_counts() / grouped["water_point_condition"].get_group("broken").count()
print(rank_by_percentage)
答案 0 :(得分:0)
您需要按ommunities_villages
分组,而不是water_point_condition
df.groupby('communities_villages')['water_point_condition'].apply(lambda x: (x == 'broken').mean())