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?

mixalis31
  • 3
  • 2

2 Answers2

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')
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
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) 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Simon Houdayer
  • 163
  • 1
  • 1
  • 10