从 CSV 文件读取和聚合数据 [英] Read and aggregate data from CSV file

查看:48
本文介绍了从 CSV 文件读取和聚合数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式如下的数据文件:

I have a data file with the following format:

name,cost1,cost1,cost1,cost2,cost3,cost3,
X,2,4,6,5,6,8,
Y,0,3,6,5,4,6,
.
.
....

现在,我想做的是将其转换为字典字典,使得

Now, what I would like to do is to convert this to a dictionary of dictionaries such that

{'X', {'cost1': 4, 'cost2':5, 'cost3':7}},{'Y', {'cost1': 3, 'cost2':5, 'cost3':5}}....

其中每个键的值是数据文件中的平均值.这怎么可能?

where the values of each key is the average from the data file. How could this be done?

推荐答案

基于@cphlewis 指示的更通用的版本:

A more generic version based on @cphlewis's directions:

load_data = csv.reader(open( "multientry.csv", "r" ))
header = next(load_data)
header = filter(bool,header)
categories = header
categories.pop(0)
categories = set(categories)
dofd={}
for row in load_data:
    row = filter(bool,tuple(value for  value in row))
    dofd[row[0]]={}
    for item in categories:
        val = [float(k) for k in [row[i+1] for i in [i for i, x in enumerate(header) if x == item]]]
        dofd[row[0]][item] = sum(val)/float(len(val))

这篇关于从 CSV 文件读取和聚合数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆