Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two dataframes side-by-side?

Tags:

python

pandas

is there a way to conveniently merge two data frames side by side?

both two data frames have 30 rows, they have different number of columns, say, df1 has 20 columns and df2 has 40 columns.

how can i easily get a new data frame of 30 rows and 60 columns?

df3 = pd.someSpecialMergeFunct(df1, df2) 

or maybe there is some special parameter in append

df3 = pd.append(df1, df2, left_index=False, right_index=false, how='left') 

ps: if possible, i hope the replicated column names could be resolved automatically.

thanks!

like image 790
James Bond Avatar asked May 27 '14 14:05

James Bond


People also ask

How do I merge two DataFrames in pandas horizontally?

To concatenate DataFrames horizontally in Pandas, use the concat(~) method with axis=1 .

How do I merge two DataFrames with different columns?

It is possible to join the different columns is using concat() method. DataFrame: It is dataframe name. axis: 0 refers to the row axis and1 refers the column axis. join: Type of join.


1 Answers

You can use the concat function for this (axis=1 is to concatenate as columns):

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

See the pandas docs on merging/concatenating: http://pandas.pydata.org/pandas-docs/stable/merging.html

like image 131
joris Avatar answered Oct 03 '22 09:10

joris