我可以在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'))
我為列使用了 group byclass并想使用classin x的值df.groupby('class').apply(lambda x: ??)
uj5u.com熱心網友回復:
IIUC 使用x.name:
print (df.groupby('class').apply(lambda x: x.name))
class
bird bird
mammal mammal
dtype: object
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/366359.html
