Skip to content
Advertisement

Append only matching columns to dataframe

I have a sort of ‘master’ dataframe that I’d like to append only matching columns from another dataframe to

df:
  A  B  C
  1  2  3

df_to_append:
  A  B  C  D  E
  6  7  8  9  0

The problem is that when I use df.append(), It also appends the unmatched columns to df.

df = df.append(df_to_append, ignore_index=True)
Out:
A  B  C   D   E
1  2  3  NaN NaN
6  7  8   9   0

But my desired output is to drop columns D and E since they are not a part of the original dataframe? Perhaps I need to use pd.concat? I don’t think I can use pd.merge since I don’t have anything unique to merge on.

Advertisement

Answer

Using concat join='inner

pd.concat([df,df_to_append],join='inner')
Out[162]: 
   A  B  C
0  1  2  3
0  6  7  8
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement