包含 pandas 的新列中带有列表的值计数 [英] Value Count with List in New Column that Comprised it Pandas

查看:43
本文介绍了包含 pandas 的新列中带有列表的值计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中有人打电话给各种各样的数字.这样:

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

我敢肯定,我可以创建一个for循环来计算次数并将其附加到列表中,但是我想知道是否有一种更有效的方法来执行此操作而不需要for循环.抱歉,如果格式不正确.我是这个论坛的新手.

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.

推荐答案

使用因为仅处理一列是可能的,所以在 groupby 之后使用元组和列的替代方法:

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

这篇关于包含 pandas 的新列中带有列表的值计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆