0

I am a Python beginner but i would like to ask something that i did not find in the forum.

I have multiple csv files into a folder which contain data like this (structure is identical to all of them):

File1.csv

Original
7200
118800
0
-955.8
7075
1080
115628.57
3171.4

File2.csv

Renovated
20505
4145
0
55
7075
103
22359
4145

And so on.

I would like to make a script in Python 3 that copies them to one csv file one column after another. Could you offer me please some help?

5
  • 1
    have you tried anything? Commented May 31, 2017 at 17:06
  • 1
    show the desired result Commented May 31, 2017 at 17:07
  • I would recommend looking at the pandas module and the functions to_csv and read_csv and concat. I would probably just do this with the unix tools tr and cat myself. Commented May 31, 2017 at 17:09
  • In the resulting csv file, do you want everything in one long column, or each file added into a new column? Commented May 31, 2017 at 17:14
  • The assistance of Brad Solomon was great, the solution works like a charm. Commented Jun 1, 2017 at 12:03

2 Answers 2

0

Say your files are named File1.csv, File2.csv, and File3.csv.

import pandas as pd

pieces = []
for num in [1, 2, 3]:
    s = pd.read_csv('folder/subfolder/File%d.csv' % num) # your directory
    pieces.append(s)
newcsv = pd.concat(pieces, axis=1) # this will yield multiple columns
newcsv.to_csv('folder/subfolder/newcsv.csv')
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure you have pandas installed and import it like this :

import pandas as pd

Read your two different csv using pd.read_csv():

df1=pd.read_csv('file1.csv')
df2=pd.read_csv('file2.csv')

Then add an other column to your first dataframe using (make sure it's the same size):

df1['Renovated'] = pd.Series(df2['Renovated'],index=df1.index)

In the df1 you should now have two columns do it for as many csv you have.

To finish to save your final csv with all columns just do :

df1.to_csv('finalName.csv', index=False) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.