Python:import csv,对特定单元格执行数学运算,将新列输出到新(或相同)CSV [英] Python: import csv, do math with specific cells, output new columns to new (or same) CSV

查看:1387
本文介绍了Python:import csv,对特定单元格执行数学运算,将新列输出到新(或相同)CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个工作了几天了。

I've been working on this for days-

我有一个CSV,看起来像:

I have a CSV that looks like:

...

有25行数字 - 它们都略有不同。
我想使用python导入这个CSV,做一些轻微的数学(例如,找到单元格A1和C1之间的平均值),然后打印一个新的COLUMN到一个全新的CSV文件或添加一个新的COLUMN到开始我当前的或(甚至重复的)文件。
我知道实际的数学部分很容易。

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.

这里是我试过的:
1)首先我尝试导入一个新的COLUMN, csv,将其更改为列表,读取我需要的列,然后导出到新的csv。我的问题是,当我导出到CSV它不创建列。它只是添加到一个单元格看起来像(0.111,1.002,..)。

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

我试图创建一个新的列到一个重复的文件,并添加信息到该列。我有一个类似的版本,从另一个类似的问题。这只是不工作 - 我得到错误TypeError:只能分配一个迭代

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)

我一直在这个DAYS可以有人请帮助! !?

I've been at this for DAYS can someone please help!?!?!?

我已经在MATLAB中写了所有这些,但需要将它转移到Python ... MATLAB更容易找出。

I've written all of this in MATLAB but need to transfer it over to Python... MATLAB was easier to figure out.

推荐答案

使用 pandas 。这是它的设计目的。

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

这篇关于Python:import csv,对特定单元格执行数学运算,将新列输出到新(或相同)CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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