我想从下面的name
和hours
列的行计数中得到最大值:
import pandas as pd
hours = [8,8,9,9,
8,9,10,10,
8,9,12,12,
10,11,12,12]
names = ['A', 'B', 'C', 'D'] * 4
df = pd.DataFrame({'names': names,
'hours', hours})
我的预期产出:
names hours count
A 8 3
B 9 2
C 12 2
D 12 2
我尝试过:
# This will get me the aggregated count based on names and hours
df.groupby(['names', 'hours']).size().reset_index(name='count')
# result
names hours count
A 8 3
10 1
B 8 1
9 2
11 1
C 9 1
10 1
12 2
D 9 1
10 1
12 2
# To get the max value for each names & hours group (But failed)
df.groupby(['names', 'hours']).size().reset_index(name='count').\
groupby(['names','hours']).max()
# I get the same result as I got above
使用groupby和value\u计数的另一种方法:
这个怎么样:
相关问题 更多 >
编程相关推荐