问题
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