简体   繁体   中英

Append dataframes in pandas

I have two dataframes in pandas (copy from Spyder Variable Explorer)

df1

index   0    1    2    3    4    5   6
0       Loc 0.0  0.0  0.0  0.25 0.0  light
1       Loc 0.0  0.0  0.0  0.25 0.0  light
2       Loc 0.0  0.0  0.0  0.25 0.0  light
3       Loc 0.0  0.0  0.0  0.25 0.0  light

df2

index   0       1      2    3   4   5   6
0       DCos  -0.25  -0.2  0.9  nan nan nan
1       DCos  -0.25   0.2  0.9  nan nan nan
2       DCos   0.25  -0.2  0.9  nan nan nan
3       DCos   0.25   0.2  0.9  nan nan nan

I would like to append dataframe 2 to dataframe 1, to have

index   0    1    2    3    4    5   6      7       8     9    10   11  12  13                                         
0       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos  -0.25  -0.2  0.9  nan nan nan
1       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos  -0.25   0.2  0.9  nan nan nan
2       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos   0.25  -0.2  0.9  nan nan nan
3       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos   0.25   0.2  0.9  nan nan nan

I have tried

df1.join.(df2)

but df1 was not changed. I know there is an append function described in the documentation, but in only appends rows. Is there a way to append columns ?

Disclaimer : I do not have 50 rep points to comment. So this answer is just a comment as EdChum has given the correct answer.

You should take a look at the documentation of the various types of concat, merge and join here .

If your trying to concatenate the two dataframes using the index, you simply need:

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

This will place the second dataframe(df2) next to the first one(df1) where the indexes match.

If you want to concatenate without being index-sensitive, try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM