我有一个简单的 Pandas 数据框,我希望使用某个列对其进行分组。df 看起来像下面的那个。
Color Car
R Ford
O Kia
Y Mazda
R Chevrolet
我想对“颜色”进行分组,因此生成的 df 将是:
Color Car
R Ford, Chevrolet
O Kia
Y Mazda
使用pandas groupby 似乎很容易。我的代码如下所示:
df = df.groupby(['Color'])
但我收到以下错误:
Cannot access callable attribute 'iloc' of 'DataFrameGroupBy' objects, try using the 'apply' method
为什么 groupby 不起作用?这似乎是最基本的操作,哪个 groupby 最适合使用?
ibeautiful
相关分类