[英]Use Pandas.groupby to select a row in a group with some criteria?
I have a dataframe like this 我有一个这样的数据框
data = pd.DataFrame({'name': [1, 2, 2, 2, 3 , 3, 4],
'type': ['A', 'B', 'B', 'C', 'B', 'C', 'A'],
'hist' : [1,2,1,3,0,3,4]})
data
Out[1]:
hist name type
0 1 1 A
1 2 2 B
2 1 2 B
3 3 2 C
4 0 3 B
5 3 3 C
6 4 4 A
I would expect this data becomes like 我希望这些数据变得像
data
Out[2]:
hist name type
0 1 1 A
1 2 2 B
2 3 2 C
3 0 3 B
4 3 3 C
5 4 4 A
It means that I firstly apply groupby(['name', 'type'])
, then for each group, I want to select column which have highest value in columns hist. 这意味着我首先应用
groupby(['name', 'type'])
,然后对于每个组,我要选择在hist列中具有最高值的列。 For example, I get idea from 例如,我从
gps = data.groupby(['name', 'type'])
keys = gps.groups.viewkeys()
Out[3]: dict_keys([(3L, 'C'), (3L, 'B'), (2L, 'B'), (2L, 'C'), (1L, 'A'), (4L, 'A')])
gps.get_group(list(keys)[2])
Out[4]:
hist name type
1 2 2 B
2 1 2 B
My approach is 我的方法是
def mfunc(df):
if len(df) > 1:
idx = df['hist'].idxmax()
l_idx = df.index.tolist()
rm_list = list(set(l_idx) - set([idx]))
df.drop(pd.Int64Index(rm_list), inplace=True)
else:
pass
result = data.groupby(['name', 'type']).apply(mfunc)
But result
is an empty DataFrame. 但是
result
是一个空的DataFrame。
Could you help me to use apply function in this situation? 在这种情况下,您能帮我使用Apply功能吗? Thanks
谢谢
This might be a more efficient approach: 这可能是一种更有效的方法:
data.groupby(['name', 'type'])['hist'].max()
Alternatively, you could do this: 或者,您可以这样做:
data.sort('hist', ascending=False).drop_duplicates(subset=['name', 'type'])
You could use pivot_table
for that purpose: 您可以出于此目的使用
pivot_table
:
df1 = pd.pivot_table(data, index=['name', 'type'], aggfunc=max).reset_index()
In [20]: df1
Out[20]:
name type hist
0 1 A 1
1 2 B 2
2 2 C 3
3 3 B 0
4 3 C 3
5 4 A 4
If you need that particular order of your columns you could reorder the result: 如果需要按特定顺序排列列,则可以对结果重新排序:
In [33]: df2 = df1[['hist', 'name', 'type']]
In [34]: df2
Out[34]:
hist name type
0 1 1 A
1 2 2 B
2 3 2 C
3 0 3 B
4 3 3 C
5 4 4 A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.