Value Count with List in New Column that Comprised it Pandas [duplicate]

五迷三道 提交于 2021-01-28 08:13:15

问题


I have a dataframe with individuals who called a variety of numbers. As so:

Person          Called
A                 123
B                 123
C                 234

I need to create a new dataframe that makes a list of people who called that number and the count. Like this:

Persons         Called         Count
A, B             123             2
C                234             1

I'm pretty sure I can just create a for loop that counts the number of times and appends them to a list, but I was wondering if there's a more efficient way to do this without a for loop. Apologies if the formatting is incorrect. I'm new to the forum.


回答1:


Use name aggregations with GroupBy.agg:

df1 = (df.groupby('Called')
         .agg(Persons = ('Person',','.join), 
              Count=('Person','size'))
         .reset_index())
print (df1)
   Called Persons  Count
0     123     A,B      2
1     234       C      1

Because processing only one column is possible use alternative with tuples and column after groupby:

df1 = (df.groupby('Called')['Person']
         .agg([('Persons', ','.join), 
               ('Count','size')])
         .reset_index())
print (df1)
   Called Persons  Count
0     123     A,B      2
1     234       C      1


来源:https://stackoverflow.com/questions/61322788/value-count-with-list-in-new-column-that-comprised-it-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!