'Pandas - merge multiple dataframes into main df
I have several (side) dataframes that each only have one column and one (main) df that has stacked, unique values of the dfs.
Code for 'main' df:
stacked_df = pd.concat([df_1,df_2,df_3,df_n], axis=0)
uniques = stacked_df.drop_duplicates(subset='key', keep='first').reset_index(drop=True)
Goal is to add the 'side dfs' into 'uniques', ie match values in df_1 to df_n against values in uniques and create a new column with values/ nans per df_n. As follows:
result = pd.DataFrame()
result['uniques'] = uniques
result['df1'] = result.merge(df1, on='key', how='inner')
That works fine for the first df, but I get an error when trying to expand it as follows:
result = pd.DataFrame()
result['uniques'] = uniques
result['df1'] = result.merge(df1, on='key', how='inner')
result['df2'] = result.merge(df2, on='key', how='inner')
Error: ValueError: Wrong number of items passed 2, placement implies 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
