首页 > 解决方案 > 通过 Pandas 中的字符串列聚合数据框

问题描述

我有一个如下所示的数据框:

dfB
name        value        country
benzene     spice        Australia
benzene     spice        Australia
benzene     spice        Australia
benzene     herbs        Australia
benzene     herbs        Americas
benzene     anise        Poland
methyl      herbs
methyl      herbs        Americas        
methyl      spice        Americas
alcohol     spice        Germany
alcohol     spice        Germany

我想创建一个不同的数据框,它是国家列的聚合,如下所示:

dfB
name        value        country        count
benzene     spice        Australia      3
benzene     herbs        Australia      1
benzene     herbs        Americas       1
benzene     anise        Poland         1
methyl      herbs                       1
methyl      herbs        Americas       1 
methyl      spice        Americas       1
alcohol     spice        Germany        2

这个想法是聚合国家列并为每个唯一的“名称”和“值”组合为国家列创建一个计数。如果有空白或楠,他也应该区别对待。

我尝试使用 groupby:

grouped = dfB.groupby(["name", "value", "country"]).agg({"country": "count"})

但它似乎并没有按照我的意图创建数据框。我怎样才能做到这一点?

标签: pythonpandasdataframepandas-groupbyaggregation

解决方案


使用value_countsgroupby不修改顺序:

out = dfB.value_counts(["name", "value", "country"], sort=False, dropna=False) \
         .rename('count').reset_index()
out.loc[out['country'].isna(), 'count'] = 1

out1 = dfB.groupby(["name", "value", "country"], sort=False, dropna=False) \
         .size().reset_index(name='count')
out1.loc[out1['country'].isna(), 'count'] = 1
>>> out
      name  value    country  count
0  alcohol  spice    Germany      2
1  benzene  anise     Poland      1
2  benzene  herbs   Americas      1
3  benzene  herbs  Australia      1
4  benzene  spice  Australia      3
5   methyl  herbs   Americas      1
6   methyl  herbs        NaN      1
7   methyl  spice   Americas      1

>>> out1
      name  value    country  count
0  benzene  spice  Australia      3
1  benzene  herbs  Australia      1
2  benzene  herbs   Americas      1
3  benzene  anise     Poland      1
4   methyl  herbs        NaN      1
5   methyl  herbs   Americas      1
6   methyl  spice   Americas      1
7  alcohol  spice    Germany      2

推荐阅读