pandas 没有聚合函数的分组依据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42901609/
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 03:14:12  来源:igfitidea点击:

Group by without an aggregate function

pythonpandaspandasql

提问by zoran119

I've seen a pandasqlquery like this:

我见过这样的pandasql查询:

df = pd.DataFrame({'A': [1, 2, 2], 'B': [3, 4, 5]})
sqldf('select * from df group by A', locals())

This gives:

这给出:

   A  B
0  1  3
1  2  6

I find it really weird to have a group by without an aggregate function, but can anyone tell me which function is used on the aggregated columns to reduce multiple values into one?

我发现没有聚合函数的 group by 真的很奇怪,但是谁能告诉我在聚合列上使用哪个函数将多个值减少为一个?

回答by Andrew L

It looks like the groupby method you're looking for is last():

看起来您正在寻找的 groupby 方法是last()

df = pd.DataFrame({'A': [1, 2, 2], 'B': [3, 4, 5]})
df.groupby('A', as_index=False).last()

Output:

输出:

   A  B
0  1  3
1  2  5

I'm saying this assuming the 5 was a typo (see my comment above) and meant to be 6.

我说这个假设 5 是一个错字(见我上面的评论)并且是 6。