聚合 pandas
Aggregating with pandas
我有一个包含两列的 pandas 数据集:
- Poblacion(城市)
- patologie(病理学)
我想显示每个城市最常见的 5-6 种疾病
hlt_downsampled_eng.groupby('Poblacion')['patologie'].count().nlargest(6)
我的想法是这样的输出:
Barcellona
Fever 5230
Rheum 2000
headache 300
cough 240
Tessara
diarrhea 5230
flu 1000
headache 300
cough 240
如何使用 pandas 实现它?
我知道我应该这样做:
每个城市组 > 每个病理组 > 计算病理数 > 聚合
你可以试试groupby两次:
# value_counts makes more sense to me
(hlt_downsampled_eng.groupby('Poblacion')['patologie'].value_counts()
.groupby('Poblacion').nlargest(6) # head(6) should also work
)
我有一个包含两列的 pandas 数据集:
- Poblacion(城市)
- patologie(病理学)
我想显示每个城市最常见的 5-6 种疾病
hlt_downsampled_eng.groupby('Poblacion')['patologie'].count().nlargest(6)
我的想法是这样的输出:
Barcellona
Fever 5230
Rheum 2000
headache 300
cough 240
Tessara
diarrhea 5230
flu 1000
headache 300
cough 240
如何使用 pandas 实现它?
我知道我应该这样做: 每个城市组 > 每个病理组 > 计算病理数 > 聚合
你可以试试groupby两次:
# value_counts makes more sense to me
(hlt_downsampled_eng.groupby('Poblacion')['patologie'].value_counts()
.groupby('Poblacion').nlargest(6) # head(6) should also work
)