我想数一数一个人在一个数据帧中出现的时间。
假设我们的数据集是-
dic = {'firstname':['John','John','John','John','John','Susan','Mike',
'Mike','Jacob','David','Jacob','David','Jacob','David','Mike'],
'lastname':['Smith','Smith','Adams','Adams','Adams','Wilson',
'Jones','Jones','White','Miller','Peterson','Miller','White',
'Miller','Jones']}
df = pd.DataFrame(dic)
print(df)输出-
firstname lastname
0 John Smith
1 John Smith
2 John Adams
3 John Adams
4 John Adams
5 Susan Wilson
6 Mike Jones
7 Mike Jones
8 Jacob White
9 David Miller
10 Jacob Peterson
11 David Miller
12 Jacob White
13 David Miller
14 Mike Jones我想数一数一个人在这个数据中按名字和姓氏设置的次数。
期望输出-
firstname lastname count
0 John Smith 2
1 John Adams 3
2 Susan Wilson 1
3 Mike Jones 3
4 Jacob White 2
5 David Miller 3
6 Jacob Peterson 1发布于 2020-06-12 17:24:05
尝尝这个,
In [22]: df.groupby(['firstname', 'lastname']).size().reset_index(name='count')
Out[22]:
firstname lastname count
0 David Miller 3
1 Jacob Peterson 1
2 Jacob White 2
3 John Adams 3
4 John Smith 2
5 Mike Jones 3
6 Susan Wilson 1https://stackoverflow.com/questions/62349338
复制相似问题