如何在CSV中执行简单计算并将结果附加到文件

时间:2013-10-07 00:03:05

标签: python csv python-2.7 append

我有一个csv,其中包含38个数据列,我想要找到的是我们要做的是,将列11除以第38列,并将此数据附加到每行的末尾。缺少csv的标题行(第1行)

如果我能够获得可以执行此操作的代码片段,我将能够操作相同的代码来执行许多类似的功能。

我的尝试涉及编辑一些为其他东西设计的代码。 见下文:

from collections import defaultdict

class_col = 11
data_col = 38

# Read in the data
with open('test.csv', 'r') as f:
    # if you have a header on the file
    # header = f.readline().strip().split(',')
    data = [line.strip().split(',') for line in f]

# Append the relevant sum to the end of each row
for row in xrange(len(data)):
    data[row].append(int(class_col)/int(data_col))

# Write the results to a new csv file
with open('testMODIFIED2.csv', 'w') as nf:
    nf.write('\n'.join(','.join(row) for row in data))

任何帮助将不胜感激。谢谢SMNALLY

2 个答案:

答案 0 :(得分:4)

import csv

with open('test.csv', 'rb') as old_csv:
    csv_reader = csv.reader(old_csv)
    with open('testMODIFIED2.csv', 'wb') as new_csv:
        csv_writer = csv.writer(new_csv)
        for i, row in enumerate(csv_reader):
            if i != 0:
                row.append(float(row[10]) / float(row[37]))
                csv_writer.writerow(row)

答案 1 :(得分:0)

使用pandas:

import pandas
df = pandas.read_csv('test.csv') #assumes header row exists
df['FRACTION'] = 1.0*df['CLASS']/df['DATA'] #by default new columns are appended to the end
df.to_csv('out.csv')