[英]Passing a function in pandas agg method of a groupby object
This is in google colab这是在谷歌colab
import pandas as pd
df = pd.DataFrame({'A': ['state1', 'state2', 'state1', 'state1', 'state2'],
'B': [1, 2, 3, 4, 5],
'C': [10, 11, 12, 13, 14]})
print(df)
A B C
0 state1 1 10
1 state2 2 11
2 state1 3 12
3 state1 4 13
4 state2 5 14
This is the DataFrame.这是 DataFrame。 If we use the built-in 'max' function with the agg
function it is working fine.如果我们使用内置的'max' function 和agg
function 它工作正常。
df.groupby('A', as_index=False)['C'].agg('max')
A C
0 state1 13
1 state2 14
But if we use a lambda
function is raising an error但是如果我们使用lambda
function 会引发错误
df.groupby('A', as_index=False)['C'].agg(lambda x: x.max())
AttributeError: 'Series' object has no attribute 'columns'
Why is this raising a error?为什么这会引发错误? What is the difference between passing a lambda function and a built-in function in the agg
method?在agg
方法中传递 lambda function 和内置 function 有什么区别?
Even for me it is not working in jupyter notebook also即使对我来说,它也不能在 jupyter notebook 中工作
This works for me:这对我有用:
df.groupby('A', as_index=False)[['C']].agg(lambda x: x.max())
Previously when I run the question for the first time the pandas version was 1.0.5.以前当我第一次运行这个问题时,pandas 版本是 1.0.5。
But after then i updated the version to 1.1.0 and ran the same thing and it works fine.但之后我将版本更新为 1.1.0 并运行相同的东西,它工作正常。
!pip install pandas==1.1.0
import pandas as pd
df = pd.DataFrame({'A': ['state1', 'state2', 'state1', 'state1', 'state2'],
'B': [1, 2, 3, 4, 5],
'C': [10, 11, 12, 13, 14]})
print(df.groupby('A', as_index=False)['C'].agg(lambda x: x.max()))
A C
0 state1 13
1 state2 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.