在Python中是否可以从单独的csv复制数据并将其放在另一个csv的特定列中?
我只想拍摄每个csv文件的第一列file1.csv
file2.csv
file3.csv
File1.csv Column A
File2.csv Column A
File3.csv Column A
并且只有一个output.csv
,其中所有csv的column A
数据都写入了自己的列
output.csv Column A (file1)
output.csv Column B (file2)
output.csv Column C (file3)
以下是我对过滤部分的看法,不知道如何为writer
csvinputlist = ['file1.csv','file2.csv','file3.csv']
for csvinput in csvinputlist
reader = csv.reader(open(r'{0}'.format(csvinput)), delimiter=',')
filtered = filter(lambda p:column A == p[0], reader)
csv.writer(open(output.csv ???????
答案 0 :(得分:2)
您可以使用pandas
:
import pandas as pd
csvinputlist = ['file1.csv','file2.csv','file3.csv']
csvinput = csvinputlist
df = pd.DataFrame()
for i, csvinput in enumerate(csvinputlist):
reader = pd.read_csv(csvinput)
df['A{}'.format(i+1)]=reader['A']
df.to_csv('constructed.csv')
其中' A'表示要读取的列的列标题。如果您的列没有标题,您可以按如下方式分配它们:
import pandas as pd
csvinputlist = ['file1.csv','file2.csv','file3.csv']
csvinput = csvinputlist
headers=['A', 'B', 'C'] # continue for as many column that are in the file
df = pd.DataFrame()
for i, csvinput in enumerate(csvinputlist):
reader = pd.read_csv(csvinput, header=None, names=headers)
df['A{}'.format(i+1)]=reader['A']
df.to_csv('constructed.csv')