Pandas Dataframe:如何在其他列中添加具有出现次数的列 [英] Pandas Dataframe: how to add column with number of occurrences in other column
本文介绍了Pandas Dataframe:如何在其他列中添加具有出现次数的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须遵循df:
Col1 Col2
test Something
test2 Something
test3 Something
test Something
test2 Something
test5 Something
我想得到
Col1 Col2 Occur
test Something 2
test2 Something 2
test3 Something 1
test Something 2
test2 Something 2
test5 Something 1
我尝试使用:
df["Occur"] = df["Col1"].value_counts()
但这没有帮助.我的Occur栏充满了"NaN"
But it didn't help. I've got Occur column full of 'NaN'
推荐答案
groupby
,然后应用Col2上的> transform
返回其索引与原始df对齐的Series,因此您可以将其添加为列:
groupby
on 'col1' and then apply transform
on Col2
to return a Series with its index aligned to the original df so you can add it as a column:
In [3]:
df['Occur'] = df.groupby('Col1')['Col2'].transform(pd.Series.value_counts)
df
Out[3]:
Col1 Col2 Occur
0 test Something 2
1 test2 Something 2
2 test3 Something 1
3 test Something 2
4 test2 Something 2
5 test5 Something 1
这篇关于Pandas Dataframe:如何在其他列中添加具有出现次数的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文