【问题标题】:pandas - DataError no numeric types to aggregatepandas - DataError 没有要聚合的数字类型
【发布时间】:2019-06-23 23:59:47
【问题描述】:

我想使用groupby 计算多列的平均值。下面是一个玩具示例

df = pd.DataFrame({'company': ['dell', 'microsoft', 'toshiba', 'apple'], 
'measure': ['sales', 'speed', 'wait time', 'service'], 'category': ['laptop', 
'tablet', 'smartphone', 'desktop'], '10/6/2015': [234, 333, 456, 290], 
'10/13/2015': [134, 154, 123, 177], '10/20/2015': [57, 57, 63, 71]})

我想计算df 中日期列中每一行的平均值。我认为使用groupby 的最佳方法是更改​​列名,以便它们每个月都不唯一,如下所示:

def maybe_rename(col_name):
if re.match('\\d+/\\d+/\\d+', col_name):
    return re.split('/', col_name)[0] + re.split('/', col_name)[2]
else:
    return col_name

df = df.rename(columns = maybe_rename)

df

     company    measure    category  102015  102015  102015
0       dell      sales      laptop     234     134      57
1  microsoft      speed      tablet     333     154      57
2    toshiba  wait time  smartphone     456     123      63
3      apple    service     desktop     290     177      71

然后我尝试像这样计算mean

df = df.groupby(df.columns, axis = 1).mean()

返回如下错误:DataError: No numeric types to aggregate

我该如何解决这个问题?我想要的结果如下:

df

     company    measure    category  102015
0       dell      sales      laptop  141.66
1  microsoft      speed      tablet  181.33
2    toshiba  wait time  smartphone   214.0
3      apple    service     desktop   79.33    

【问题讨论】:

  • 你不能对分类值取平均值......首先排除它们,做df.loc[:,'102015'].mean(axis=1)
  • @yatu 如果有很多列怎么办?

标签: python pandas dataframe


【解决方案1】:

试试这个:

import pandas as pd
df = pd.DataFrame({'company': ['dell', 'microsoft', 'toshiba', 'apple'],
'measure': ['sales', 'speed', 'wait time', 'service'], 'category': ['laptop',
'tablet', 'smartphone', 'desktop'], '10/6/2015': [234, 333, 456, 290],
'10/13/2015': [134, 154, 123, 177], '10/20/2015': [57, 57, 63, 71]})

columns_to_average = ['10/6/2015','10/20/2015','10/13/2015']
df['means'] = df[columns_to_average].mean(axis=1)

如果您有很多日期列,我建议将其转换为时间序列数据...

tdf = df[['category','10/6/2015','10/20/2015','10/13/2015']].transpose()
tdf = tdf.rename(columns=tdf.iloc[0]).drop(tdf.index[0])
print(tdf['laptop'].mean())

【讨论】:

  • 我不明白如何将其推广到例如 n 列。
猜你喜欢
  • 1970-01-01
  • 2021-09-22
  • 2020-09-20
  • 2021-08-07
  • 1970-01-01
  • 2017-01-06
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多