gpt4 book ai didi

python - 如何使用 Pandas 选择两组中的前 N ​​组并将第二组的其余部分聚合到 "Others"中?

转载 作者:行者123 更新时间:2023-12-01 23:22:52 25 4
gpt4 key购买 nike

我有一个包含产品、价格、类别和县的数据。我使用此代码来计算每个县每个类别的产品数量:

df_count = df.groupby(['County','Category']).size().reset_index(name='counts')

我的数据框现在看起来像这样:

<表类="s-表"><头><日> 县类别计数<正文>0布莱金厄配饰与 watch 351布莱金厄音视频1012布莱金厄自行车783布莱金厄船零件和配件654布莱金厄船143............657东约特兰雪地摩托零件和配件2658东约特兰雪地摩托5659东约特兰运动休闲装备335660东约特兰工具102661东约特兰卡车与建筑66

662 行 × 3 列

有21个县,32个类别。数量是一个类别中的产品数量。并非所有类别在一个县都是必需的。

我想要一个新的数据框,其中包含每个县的前 N ​​个(例如 2 个)最大类别,并将其余部分汇总到“其他”。我想要每个县都这样,它看起来像这样:

<表类="s-表"><头>县类别计数<正文>布莱金厄船143布莱金厄音视频101布莱金厄其他178.........东约特兰运动休闲装备335东约特兰工具102东约特兰其他175

我看过以前对数组做类似的帖子

How to group "remaining" results beyond Top N into "Others" with pandas

Sort top N and group 'others' in pandas df

并尝试了这个

# group by & sort descending
df_sorted=df_count.groupby(['County','Category']).sum().sort_values('counts', ascending=False).reset_index()

# rename rows other than top-n to 'Others'
x_sorted.groupby('County').loc[x_sorted.index >=3, 'Category'] = 'Others'

还有这个

df_count.sort_values(by=['counts'], ascending=False).groupby('County').head(2).sort_values(by=['County']).reset_index(drop=True)

#not_top2 = df.groupby('Version').sum().sort('Value', ascending=False).index[2:]
not_top2 = x.groupby(['County','Category']).sum().sort_values('counts', ascending=False).index[2:]

dfnew = x.replace(not_top2, 'Other')

dfnew.groupby(['County','Category']).sum()

但没有成功获得所需的输出。

非常感谢任何帮助或指导!

最佳答案

您可以使用以下步骤序列获得最终输出,我认为这非常简单。

为了便于理解,我将在代码和每行输出中添加注释。

# Grab top 2 largest caterogies of each country
top_two = df.groupby('County').apply(lambda x: x.nlargest(2, 'counts')).reset_index(drop=True)

>>> top_two
County Category counts
0 Blekinge Boats 143
1 Blekinge Audio & video 101
2 Östergötland Sports & leisure equipment 335
3 Östergötland Tools 102

# Create a dataframe with the rest of the information
df_others = df.append(df.merge(top_two,'inner')).drop_duplicates(keep=False)

>>> df_others
County Category counts
0 Blekinge Accessories & watches 35
2 Blekinge Bicycles 78
3 Blekinge Boat parts & accessories 65
5 Östergötland Snowmobile parts & accessories 2
6 Östergötland Snowmobiles 5
9 Östergötland Trucks & construction 66

# Groupby country and Sum and assign 'others' under Category in the df_others dataframe
df_others = df_others.groupby('County')['counts'].sum().reset_index()
df_others['Category'] = 'Others'

>>> df_others
County counts Category
0 Blekinge 178 Others
1 Östergötland 73 Others

最后,concat() 两个数据帧以获得最终输出:

res = pd.concat([top_two,df_others]).sort_values('County').reset_index(drop=True)
>>> res
County Category counts
0 Blekinge Boats 143
1 Blekinge Audio & video 101
2 Blekinge Others 178
3 Östergötland Sports & leisure equipment 335
4 Östergötland Tools 102
5 Östergötland Others 73

如有不明之处请回来

关于python - 如何使用 Pandas 选择两组中的前 N ​​组并将第二组的其余部分聚合到 "Others"中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67720054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com