0

假设我有一个包含以下标题和数据的文件“test.csv”:

h1  c1  h2  h3  c2
1   0   2   3   1
3   0   2   1   0
0   1   2   3   3

python中仅选择并保存感兴趣的列并丢弃所有其他列的最佳选择是什么?

假设我只对保存 h 列感兴趣,我想到了以下几点:

f = open('test.csv')
s = save('new_test.csv', data = f, saveColumns=['h1','h2','h3'])´

n = load('new_test.csv')
print n

h1  h2  h3
1   2   3
3   2   1
0   2   3
4

3 回答 3

1
f = open("test.csv")
header = {i: x for i, x in enumerate(f.readline().split())}
columns = ('h1','h2','h3')
for l in f:
    print [x for i, x in enumerate(l.split()) if header[i] in columns]
于 2013-08-08T19:02:31.857 回答
1

我找到了一种非常简单的方法:

import pandas as pd
selectColumns = ['h1','h2','h3']
table = pd.read_csv('test.csv')
tableNew = table[selectColumns]
pd.to_csv('tableNew')
于 2013-09-07T14:22:23.800 回答
0
>>> d=csv.DictReader(open("some.csv"),delimiter="\t")
>>> fields = ["h1","h2","h3"]
>>> new_rows = [[row[f] for f in fields] for row in d]
>>> d=csv.DictWriter(open("new_csv.csv","w"),fields,delimiter="\t")
>>> d.writerows(new_rows)
于 2013-08-08T18:58:39.103 回答