df
Out[87]: A B C0 a 1 41 a 2 62 b 3 5
g = df.groupby('A')
g.apply(f) #f:形参为DataFrame
g['B'].apply(f) #f:形参为Series
df[['B','C']].apply(f)#f:形参为列或行(axis=1)
series.apply(f) #f:形参为元素;相当于Series.map(f)
#以上函数均可继续传入位置参数:f(s,k1=...,k2=...,k3=....); apply(f,k1=?,k2=?,k3=?)
df.groupby('A').apply(lambda x:DataFrame({"min":x.min(),"idxmin":x.idxmin()}))
Out[95]: min idxminA a B 1 0 C 4 0b B 3 2 C 5 2g=df.groupby('A')
def f(frame):
return frame.apply(lambda s:Series([s.min(),s.max()],index=['min','max']))
g.apply(f)
Out[98]: A B CA a min a 1 4 max a 2 6b min b 3 5 max b 3 5
def f2(frame):
return frame.apply(lambda s:Series([s.min(),s.argmin()],index=['min','argmin']))
g.apply(f2)
E:\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: 'argmin' is deprecated, use 'idxmin' instead. The behavior of 'argmin'will be corrected to return the positional minimum in the future.Use 'series.values.argmin' to get the position of the minimum now. Out[104]: B CA a min 1 4 argmin 0 0b min 3 5 argmin 2 2