Read and aggregate data from CSV file

2024/10/14 9:19:25

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?

Answer
import csv
types = [str, float, float, float, float, float, float]                                            with open('multientry.csv') as f:data = csv.reader(f, delimiter=',')headers = next(data)dofd = {}for r in data:r = tuple(convert(value) for convert, value in zip(types, r))dofd[r[0]] = {}dofd[r[0]]['cost1'] = (r[1]+r[2]+r[3])/3.dofd[r[0]]['cost2'] = r[4]dofd[r[0]]['cost3'] = (r[5]+r[6])/2.print dofd
https://en.xdnf.cn/q/117972.html

Related Q&A

nltk cant using ImportError: cannot import name compat

This is my codeimport nltk freq_dist = nltk.FreqDist(words) print freq_dist.keys()[:50] # 50 most frequent tokens print freq_dist.keys()[-50:] # 50 least frequent tokensAnd I am getting this error mess…

Fitting and Plotting Lognormal

Im having trouble doing something as relatively simple as:Draw N samples from a gaussian with some mean and variance Take logs to those N samples Fit a lognormal (using stats.lognorm.fit) Spit out a n…

Is there any way to install nose in Maya?

Im using Autodesk Maya 2008 on Linux at home, and Maya 2012 on Windows 7 at work. Most of my efforts so far have been focused on the former. I found this thread, and managed to get the setup there work…

Basic python socket server application doesnt result in expected output

Im trying to write a basic server / client application in python, where the clients sends the numbers 1-15 to the server, and the server prints it on the server side console. Code for client:import soc…

creating dictionaries to list order of ranking

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people.The employee order comes from a txt file:

Python: How to use MFdataset in netCDF4

I am trying to read multiple NetCDF files and my code returns the error:ValueError: MFNetCDF4 only works with NETCDF3_* and NETCDF4_CLASSIC formatted files, not NETCDF4. I looked up the documentation a…

Pyspark: Concat function generated columns into new dataframe

I have a pyspark dataframe (df) with n cols, I would like to generate another df of n cols, where each column records the percentage difference b/w consecutive rows in the corresponding, original df co…

Mysql.connector to access remote database in local network Python 3

I used mysql.connector python library to make changes to my local SQL server databases using: from __future__ import print_function import mysql.connector as kkcnx = kk.connect(user=root, password=pass…

concurrent.futures not parallelizing write

I have a list dataframe_chunk which contains chunks of a very large pandas dataframe.I would like to write every single chunk into a different csv, and to do so in parallel. However, I see the files be…

Querying SQLite database file in Google Colab

print (Files in Drive:)!ls drive/AIFiles in Drive:database.sqlite Reviews.csv Untitled0.ipynb fine_food_reviews.ipynb Titanic.csvWhen I run the above code in Google Colab, clearly my sqlite file is pre…