是否可以在groupby apply中获取分组列的值?
Can I get the value of the grouped column in groupby apply?
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
使用了 group by 并希望在 x df.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
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
使用了 group by 并希望在 x df.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