熊猫Python中的Groupby Mean

时间:2018-10-30 19:31:25

标签: python mean pandas-groupby

我有一个由5个字段组成的csv文件。 一些样本数据:

market_name,vendor_name,price,name,ship_from 
'Greece',03wel,1.79367196,huhif,Germany
'Greece',le,0.05880975,fdfd,Germany
'Mlkio',dpg,0.11344859,fdfd,Germany 
'Greece',gert,0.18655316,,Germany
'Tu',roland,0.52856728,fdfsdv,Germany 
'ghuo',andy,0.52856728,jhjhj,Germany
'ghuo',didier,0.02085452,fsdfdf,Germany 
'arsen',roch,0.02578377,uykujkj,Germany
'arsen',dpg,0.10010169,wrefrewrf,Germany 
'arsen',dpg,0.06415609,jhgjhg,Germany
'arsen',03wel,0.02578377,gfdgb,Germany 
'giar',03wel,0.02275039,gfhfbf,Germany
'giar',03wel,0.42751765,sdgfdgfg,Germany

在此文件中,每个供应商都有多个记录。我想查找字段vendor_name的每个唯一值,并计算每个供应商的平均值price。我正在使用以下脚本:

import pandas as pd
import numpy as np
import csv
from random import randint

ds = pd.read_csv("sxedonetoimo2.csv", 
                 dtype={"vendor_name": object, "name" : object, 
                        "ship_from" : object, "price": object})

ds['ship_from']=ds.ship_from.str.lower()
print(ds.dtypes)
pd.to_numeric(ds['price'], errors='coerce')

d = { 'name': pd.Series.nunique,
      'ship_from' : lambda x: randint(1,2) if (x==('eu'or'europe'or'eu'or'europeanunion'or'worldwide'or'us'or'unitedstates'or'usa'or'us'or'ww'or'wweu'or'euww'or'internet')).any() else randint(3,20)
      ,'price': ds.groupby('vendor_name')['price'].mean()
      }

result = ds.groupby('vendor_name').agg(d)

result.to_csv("scaled_upd.csv")

但是我收到此错误:

  

提高DataError('没有要聚合的数字类型')   pandas.core.base.DataError:没有要聚合的数字类型

在我的csv文件中,字段价格的值只是数字。如果我将该字段的类型更改为float,则会引发一个错误,即无法解析特定的字符串。我真的很困惑。有帮助吗?

1 个答案:

答案 0 :(得分:1)

只需使用read_csv()groupby()agg()

import pandas as pd

df = pd.read_csv('test.csv').groupby('vendor_name') /
    .agg({'price': 'mean', 'name': lambda x: x.nunique()})

收益:

                price  name
vendor_name                
03wel        0.567431     4
andy         0.528567     1
didier       0.020855     1
dpg          0.092569     3
gert         0.186553     0
le           0.058810     1
roch         0.025784     1
roland       0.528567     1