这个问题在这里已有答案:
这个问题可能与pandas无关,但是当一个函数作为另一个函数中的参数传递时,python如何处理,我不确定。
无论如何,请注意以下代码的意图,问题是三重报价:
import pandas as pd
import numpy as np
"""given:"""
df = pd.DataFrame(
{
'a': [100]*2+[200]*2,
'b': np.arange(11,55,11),
}
)
gb = df.groupby('a', as_index=0)
"""what's the pandanic way of writing the following working code:"""
gb.agg( {'b': lambda sr: sr.iat[0]})
def foo(sr, arg):
return sr.sum() + arg
gb.agg( {'b': lambda sr: foo(sr, 888)} )
"""into the following pseudo, but not working, code:"""
gb.agg( {'b': iat[0]} )
gb.agg( {'b': foo( ,888)} )
那就是nth
gb.nth(0)
Out[503]:
a b
0 100 11
2 200 33