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(i.e. 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?.