user3225439
user3225439

Reputation: 19

reading multiple csv file into a big pandas data frame by appending the columns of different size

so i am creating some data frame in a loop and save them as a csv file. The data frame have the same columns but different length. i would like to be able to concatenate these data frames into a single data frame that has all the columns something like

df1 A B C 0 0 1 2 1 0 1 0 2 1.2 1 1 3 2 1 2

df2 A B C 0 0 1 2 1 0 1 0 2 0.2 1 2

df3 A B C 0 0 1 2 1 0 1 0 2 1.2 1 1 3 2 1 4 4 1 2 2 5 2.3 3 0

i would like to get something like

df_big A B C A B C A B C 0 0 1 2 0 1 2 0 1 2 1 0 1 0 0 1 0 0 1 0 2 1.2 1 1 0.2 1 2 1.2 1 1 3 2 1 2 2 1 4 4 1 2 2 5 2.3 3 0 is this something that can be done in pandas?

Upvotes: 1

Views: 978

Answers (1)

unutbu
unutbu

Reputation: 879201

You could use pd.concat:

df_big = pd.concat([df1, df2, df3], axis=1)

yields

     A   B   C    A   B   C    A  B  C
0  0.0   1   2  0.0   1   2  0.0  1  2
1  0.0   1   0  0.0   1   0  0.0  1  0
2  1.2   1   1  0.2   1   2  1.2  1  1
3  2.0   1   2  NaN NaN NaN  2.0  1  4
4  NaN NaN NaN  NaN NaN NaN  1.0  2  2
5  NaN NaN NaN  NaN NaN NaN  2.3  3  0

Upvotes: 1

Related Questions