假设我有一个包含以下标题和数据的'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
答案 0 :(得分: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]
答案 1 :(得分:1)
我找到了一种非常简单的方法:
import pandas as pd
selectColumns = ['h1','h2','h3']
table = pd.read_csv('test.csv')
tableNew = table[selectColumns]
pd.to_csv('tableNew')
答案 2 :(得分: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)