How do I join together 4 dataframes? The names of the dataframes are called df1, df2, df3, and df4. They are all the same column size and I am trying to use the 'inner' join.
How would I modify this code to make it work for all four?
I tried using this code and it worked to combine two of them, but I could not figure out how to write it to work for all four dataframes.
dfJoin = df1.join(df2,how='inner')
print(dfJoin)
CodePudding user response:
You just have to chain together the joins.
dfJoin = df1.join(df2, how="inner", on="common_column") /
.join(df3, how="inner", on="common_column") /
.join(df4, how="inner", on="common_column")
or if you have more than 4, just put them in a list df_list
and iterate through it.
CodePudding user response:
Joining or appending multiple DataFrames in one go can be down with pd.concat()
:
list_of_df = [df1, df2, df3, df4]
df = pd.concat(list_of_df, how=“inner”)
Your question does now state if you want them merged column- or index-wise, but since your state that they have the same number of columns, I assume you wish to append said DataFrames. For this case the code above works. If you want to make a wide DataFrame, change the attribute axis
to 1.