我可以获取pandas groupby中apply的分组列的值吗? 例如,
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
使用了 group by 并希望在 x 中使用 class
的值 df.groupby('class').apply(lambda x : ??)
最佳答案
IIUC 使用x.name
:
print (df.groupby('class').apply(lambda x: x.name))
class
bird bird
mammal mammal
dtype: object
关于pandas - 我可以获取groupby apply中分组列的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70112968/