简体   繁体   中英

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:

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, 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?.

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 .

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)

Then do this:

import pandas as pd
pd.concat([df1, df2], axis=1)

Or merge them (pd.merge())

See this question:

Combine two Pandas dataframes with the same index

Use the csv standard python module

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))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM