2

I have 3 different csv files and I'm looking for concat the values. The only condition I need is that the first csv dataframe must be in column A of the new csv, the second csv dataframe in the column B and the Thirth csv dataframe in the C Column. The quantity of rows is the same for all csv files. Also I need to change the three headers to ['año_pasado','mes_pasado','este_mes']

import pandas as pd

df = pd.read_csv('año_pasado_subastas2.csv', sep=',')

df1 = pd.read_csv('mes_pasado_subastas2.csv', sep=',')

df2 = pd.read_csv('este_mes_subastas2.csv', sep=',')

df1
>>>
Subastas
166665859
237944547
260106086
276599496
251813654
223790056
179340698
177500866
239884764
234813107

df2
>>>
Subastas
212003586
161813617
172179313
209185016
203804433
198207783
179410798
156375658
130228140
124964988

df3
>>>
Subastas
142552750
227514418
222635042
216263925
196209965
140984000
139712089
215588302
229478041
222211457

The output that I need is:

año_pasado,mes_pasado,este_mes
166665859,124964988,142552750
237944547,161813617,227514418
260106086,172179313,222635042
276599496,209185016,216263925
251813654,203804433,196209965
223790056,198207783,140984000
179340698,179410798,139712089
177500866,156375658,215588302
239884764,130228140,229478041
234813107,124964988,222211457

1 Answer 1

1

I think you need concat of Series created by squeeze=True if one column data only or selecting columns and for new columns names use parameter keys:

df = pd.read_csv('año_pasado_subastas2.csv', squeeze=True)
df1 = pd.read_csv('mes_pasado_subastas2.csv', squeeze=True)
df2 = pd.read_csv('este_mes_subastas2.csv', squeeze=True)

cols = ['año_pasado','mes_pasado','este_mes']
df = pd.concat([df, df1, df2], keys = cols, axis=1)

Or:

df = pd.read_csv('año_pasado_subastas2.csv')
df1 = pd.read_csv('mes_pasado_subastas2.csv')
df2 = pd.read_csv('este_mes_subastas2.csv')

cols = ['año_pasado','mes_pasado','este_mes']
df = pd.concat([df['Subastas'], df1['Subastas'], df2['Subastas']], keys = cols, axis=1)

print (df)
   año_pasado  mes_pasado   este_mes
0   166665859   212003586  142552750
1   237944547   161813617  227514418
2   260106086   172179313  222635042
3   276599496   209185016  216263925
4   251813654   203804433  196209965
5   223790056   198207783  140984000
6   179340698   179410798  139712089
7   177500866   156375658  215588302
8   239884764   130228140  229478041
9   234813107   124964988  222211457
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.