I've been working on this for days-
I have a CSV that looks like:
COL A || COL B|| COL C||
0.1 || 0.0 || 0.5 ||
0.4 || 60 || 0.6 ||
0.3 || -60 || 0.5 ||
...
0.2 || -60 || 0.4 ||
There are 25 rows of numbers- they all vary slightly.
I want to import this CSV using python, do some slight math (ex. finding the avg between cell A1 and C1) then either print a new COLUMN to a whole new CSV file or add a new COLUMN to the beginning of my current or (even duplicated) file.
I know the the actual math part is easy. It's the importing, manipulation, then exporting a new COLUMN that I just cannot get.
Here's what I've tried:
1) First I tried importing the csv, changing it to a list, reading the columns I need then exporting to a new csv. The issue I have is that when I export to the CSV it doesn't create columns. It just adds things to a single cell that look like (0.111, 1.002, ..).
import csv
ofile=open('duplicate.csv', "w")
writer=csv.writer(ofile, delimiter=',')
with open('/Users/myCSV.csv', 'rb') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
avg=[]
high=[]
#average number
for i in range(1,25):
x=float(mycsv[i][16])
avg.append(x)
#print avg
average=zip(avg)
#highest number
for i in range(1,25):
x=float(mycsv[i][15])
high.append(x)
#print high
highest=zip(high)
print highest
writer.writerow([average,highest])
ofile.close()
2)Then I tried just creating a new column to a duplicate file and adding information into that column. I got a similar version of this from another similar question. This just doesn't work- I get the error "TypeError: can only assign an iterable"
import csv
infilename = r'/Users/myCSV.csv'
outfilename = r'/Users/myCSV_duplicate.csv'
with open(infilename, 'rb') as fp_in, open(outfilename, 'wb') as fp_out:
reader = csv.reader(fp_in, delimiter=",")
writer = csv.writer(fp_out, delimiter=",")
headers = next(reader) # read title row
headers[0:0] = ['avg']
writer.writerow(headers)
for row in reader:
for i in range(1,25):
mycsv=list(reader)
row[0:0] = float(mycsv[i][15])
writer.writerow(row)
I've been at this for DAYS can someone please help!?!?!?
I've written all of this in MATLAB but need to transfer it over to Python... MATLAB was easier to figure out.
解决方案
Use pandas. This is what it is designed for.
import pandas as pd
df = pd.read_csv('test.csv', sep='\|\|', usecols=[0, 1, 2])
df['avg'] = df.loc[:, ('COL A', 'COL C')].mean(axis=1)
df.to_csv('test2.csv', index=False)
df.to_csv('tes3.csv', index=False, columns='avg')