python计算csv列平均值_Python:import csv,对特定单元格执行数学运算,将新列输出到新(或相同)CSV...

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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值