简体   繁体   English

如何使用python将目录中所有.csv文件的右侧连接在一起?

[英]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.

相关问题 如何将目录中的所有文件拼接成一个文件 - How to concatenate all files in the directory into one file python脚本将目录中的所有文件连接成一个文件 - python script to concatenate all the files in the directory into one file 如何串联目录中的所有CSV,并使用Python将CSV名称添加为列 - How to concatenate all CSVs in a directory, adding CSV name as a column with Python Python pandas 将目录中的所有 tsv 文件连接到新文件 - Python pandas concatenate all tsv files from directory to new file Python - 在特定目录中连接CSV文件 - Python - Concatenate CSV files in a specific directory 将文件夹目录中的所有.csv文件复制到python中的一个文件夹 - Copy all .csv files in a directory of folders to one folder in python 将不同文件夹中的多个 csv 文件连接到 python 中的一个 csv 文件中 - Concatenate multiple csv files from different folders into one csv file in python 如果我有一个CSV文件的Python列表,如何将它们全部合并为一个巨型CSV文件? - If I have a Python list of CSV files, how do I merge them all into one giant CSV file? 如何使用Python将多个Javascript文件连接为一个文件 - How to concatenate several Javascript files into one file using Python 如何1.将4,550个dbf文件转换为csv文件2.根据名称连接文件3.将所有csv连接成一个大数据csv进行分析? - How to 1. convert 4,550 dbf files to csv files 2. concatenate files based on names 3. concatenate all csv's into one big data csv for analysis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM