我可以在apply in pandas groupby中获得分组列的值吗?例如,
df = pd.DataFrame([('bird', 389.0),
('bird', 24.0),
('mammal', 80.5),
('mammal', np.nan)],
index=['falcon', 'parrot', 'lion', 'monkey'],
columns=('class', 'max_speed'))
我对列class
使用了分组依据,并希望在xdf.groupby('class').apply(lambda x: ??)
中使用class
的值
IIUC使用x.name
:
print (df.groupby('class').apply(lambda x: x.name))
class
bird bird
mammal mammal
dtype: object