使用Pandas groupby组合d

2024-04-29 22:58:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的Pandas数据框,我希望通过使用某个列对其进行分组。df看起来像下面的那个。你知道吗

Color    Car
  R      Ford
  O      Kia
  Y      Mazda
  R      Chevrolet

我想按“颜色”分组,因此结果df将是:

Color    Car
  R      Ford, Chevrolet 
  O      Kia
  Y      Mazda

这似乎很容易使用熊猫群比。我的代码如下所示:

df = df.groupby(['Color'])

但我得到以下错误:

Cannot access callable attribute 'iloc' of 'DataFrameGroupBy' objects, try using the 'apply' method 

为什么groupby不起作用?这似乎是最基本的操作,而groupby是最适合使用的?你知道吗


Tags: 数据代码pandasdfaccess颜色错误car