【发布时间】: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()之前错过了一个括号。请检查!!