0

By utilizing pandas, I'm trying to add data from df2 to df1 by matching the ID for each row entry. However, I do not want to add information relevant to all the columns, only the columns that already exist in the initial dataframe. In the example below, I want to add the data from the color and size columns in df2, but not the length column. Additional, not all IDs from df1 will match an ID from df2, so those rows should just remain unchanged. The resultant dataframe shows the outcome I want after the "merge" of data. Something important to note is that the column exists already in the initial DF.

When I use the solution detailed here, the merged columns get added to the end of the dataframe rather than adding to existing columns: Python Pandas merge only certain columns

DF 1

ID Shape Color Size
213 square NaN NaN
2321 circle NaN NaN
2011 circle NaN NaN

DF 2

ID Length Color Size
213 long blue big
2321 short red small

Resultant DF1

ID Shape Color Size
213 square blue big
2321 circle red small
2011 circle NaN NaN

0 Answers0