【问题标题】:AttributeError: 'list' object has no attribute 'reset_index'AttributeError:“列表”对象没有属性“reset_index”
【发布时间】:2019-05-20 02:06:56
【问题描述】:

我对 pandas 非常陌生,但在使用 group by 时遇到了问题。我正在尝试按例如汽车型号和汽车价值进行分组。我希望它输出每种车型的平均价格。

model    price
------   ----
honda     2000
Toyota    3000
file = pd.read_csv('file.csv')
file2 = pd.read_csv('file2.csv')

file2['price'] = file2['price'].replace('#','')
file['price'] = file['price'].replace('#','')

new = pd.merge(file,file2, on=['col'])

new.drop(['cols'],inplace=True,axis=1)

**new.groupby(['car','price'].reset_index().groupby(['price']).mean(),as_index=True)**

我不断收到错误消息:AttributeError: 'list' object has no attribute 'reset_index'

我最初尝试过这个:

new.groupby(['car']).groupby(['price']).mean()

但它正在抛出:Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method

【问题讨论】:

  • new.groupby('car').price.mean() ?
  • 天啊,我只是拍了拍我的头。哈哈,不知道你可以这样单独使用价格..
  • 快乐编码 :-)
  • 对于第一个错误:` 'list' object has no attribute 'reset_index'` 我想你在reset_index()之前错过了一个括号。请检查!!

标签: python pandas


【解决方案1】:

您不需要使用groupby() 来表示价格。你可以简单地使用

new.groupby('car').price

访问它。

【讨论】:

    猜你喜欢
    • 2021-07-05
    • 2018-01-16
    • 2016-05-14
    • 2016-12-21
    • 2022-01-23
    • 2022-01-23
    • 2019-11-01
    • 2021-08-23
    • 2016-04-22
    相关资源
    最近更新 更多