[英]How to concatenate for the right side in one file all the .csv files of a directory with python?
I have a folder with .csv files all the files have the same ids but different contet, like this: 我有一个包含.csv文件的文件夹,所有文件都具有相同的ID,但竞争不同,如下所示:
File one: 文件一:
id, content
jdhfs_SDGSD_9403, bla bla bla bla
aadaaSDFDS__ASdas_asad_342, bla bla
...
asdkjASDAS_asdasSFSF_sdf, bla bla
File two: 文件二:
id, content
jdhfs_SDGSD_9403, string string string
aadaaSDFDS__ASdas_asad_342, string string string
...
asdkjASDAS_asdasSFSF_sdf, string string string
I would like to leave the id column but merge in one new file the content, something like this(ie generate a new file): 我想离开id列,但将内容合并到一个新文件中(例如,生成一个新文件):
id, content
jdhfs_SDGSD_9403, bla bla bla bla string string string
aadaaSDFDS__ASdas_asad_342, bla bla string string string
...
asdkjASDAS_asdasSFSF_sdf, bla bla string string string
This is what I tried: 这是我尝试的:
from itertools import izip_longest
with open('path/file1.csv', 'w') as res, \
open('/path/file1.csv') as f1,\
open('path/file1.csv') as f2:
for line1, line2 in izip_longest(f1, f2, fillvalue=""):
res.write("{} {}".format(line1.rstrip(), line2))
The problem with this is that is merging everthing in one line. 这样做的问题是将所有内容合并为一行。 Any idea of how to do this in a more pythonic way?. 是否知道如何以更Python化的方式执行此操作?
Edit: 编辑:
import pandas as pd
df1= pd.read_csv('path/file1.csv')
df2=pd.read_csv('path/file2.csv')
new_df = pd.concat([df1, df2], axis=1)
print new_df
new_df.to_csv('/path/new.csv')
Then the header was merged like this: 然后标题被合并为:
,id,content,id,content
And the content like this: 内容如下:
0jdhfs_SDGSD_9403, bla bla bla bla jdhfs_SDGSD_9403, string string string
. 0jdhfs_SDGSD_9403, bla bla bla bla jdhfs_SDGSD_9403, string string string
。
How can I get something like this?: 我如何得到这样的东西?
jdhfs_SDGSD_9403, bla bla bla bla string string string
Without the index number of the dataframe?. 没有数据帧的索引号?
read the csvs's in using pd.read_csv(FILE) 使用pd.read_csv(FILE)读取csvs
Then do this: 然后执行以下操作:
import pandas as pd
pd.concat([df1, df2], axis=1)
Or merge them (pd.merge()) 或合并它们(pd.merge())
See this question: 看到这个问题:
Combine two Pandas dataframes with the same index 结合两个具有相同索引的熊猫数据框
Use the csv
standard python module 使用csv
标准python模块
ie 即
import csv
with open(filename1) as file1, open(filename2) as file2, open(newname, "w") as newfile:
csv1 = csv.reader(file1)
csv2 = csv.reader(file2)
newcsv = csv.writer(newfile)
header = next(csv1)
next(csv2) # Skip the header
newcsv.writerow(header)
for row1, row2 in zip(csv1, csv2):
id, content1 = row1
id, content2 = row2
newcsv.writerow((id, " ".join((content1, content2))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.