pandas 通过另一列熊猫找到列组的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34908066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:32:30  来源:igfitidea点击:

find the max of column group by another column pandas

pythonpandasgroup-bymax

提问by User1090

I have a dataframe with 2 columns,

我有一个包含 2 列的数据框,

count percent  grpno.
0          14.78       1
1           0.00       2
2           8.80       3
3           9.60       4
4          55.90       4
5           0.00       2
6           0.00       6
7           0.00       5
8           6.90       1
9          59.00       4

I need to get the max of column 'count percent ' and group by column 'grpno.'. Though I tried doing the same by

我需要获得“count percent”列的最大值并按“grpno.”列分组。虽然我尝试通过

geostat.groupby(['grpno.'], sort=False)['count percent'].max()

I get the output to be

我得到的输出是

grpno.
1    14.78
2     0.00
3     8.80
4    59.00
6     0.00
5     0.00
Name: count percent, dtype: float64

But I need output to be a dataframe that has the column name modified as 'MaxOfcount percent' and 'grpno.' Can anyone help on this? Thanks

但我需要输出是一个数据框,其列名修改为“MaxOfcount percent”和“grpno”。任何人都可以帮忙吗?谢谢

回答by Stefan

res = df.groupby('grpno.')['count percent'].max().reset_index()
res.columns = ['grpno.', 'MaxOfcount percent']

   grpno.  MaxOfcount percent
0       1               14.78
1       2                0.00
2       3                8.80
3       4               59.00
4       5                0.00
5       6                0.00

You could also do it in one line:

您也可以在一行中完成:

res = df.groupby('grpno.', as_index=False)['count percent'].max().rename(columns={'count percent': 'MaxOfcount percent'})

回答by Anton Protopopov

You could use groupbywith argument as_index=False:

您可以使用groupbywith 参数as_index=False

In [119]: df.groupby(['grpno.'], as_index=False)[['count percent']].max()
Out[119]: 
   grpno.  count percent
0       1    14.78
1       2     0.00
2       3     8.80
3       4    59.00
4       5     0.00
5       6     0.00

df1 = df.groupby(['grpno.'], as_index=False)[['count percent']].max()
df1.columns = df1.columns[:-1].tolist() + ['MaxOfcount percent']

In [130]: df1
Out[130]: 
   grpno.  MaxOfcount percent
0       1               14.78
1       2                0.00
2       3                8.80
3       4               59.00
4       5                0.00
5       6                0.00