DataFrame.apply vs. group.apply vs.Series.apply

df
Out[87]:
   A  B  C
0  a  1  4
1  a  2  6
2  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  idxmin
A              
a B    1       0
  C    4       0
b B    3       2
  C    5       2

g=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  C
A            
a min  a  1  4
  max  a  2  6
b 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  C
A            
a min     1  4
  argmin  0  0
b min     3  5
  argmin  2  2

猜你喜欢

转载自www.cnblogs.com/ziheIT/p/9256213.html