Skip to content
Advertisement

pandas: append rows to another dataframe under the similar row based on multiple columns

I asked quite a similar question here but was wondering if there is a way to tackle the issue if one has to rely on multiple columns to perform the append. So the dataframes look as follows,

JavaScript

so this time, i would like to append the rows from df2 under similar rows in df1 only if the rows are similar in all col1, col2, col3. so the output is,

JavaScript

so I tried the following,

JavaScript

but I get this error,

JavaScript

Advertisement

Answer

Another solution is to use pd.merge and pd.wide_to_long:

JavaScript

Output:

JavaScript

Step by step

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement