0

I have two DataFrames

df1

UNIQUE NUMBER   NAME    
1               John    
2               Sarah   
3               Luke    
4               Bob     
5               Annah   
df2

UNIQUE NUMBER   COLOR
1               blue
3               red    
5               purple

In the dataset I have, the UNIQUE NUMBERS correspond to both the NAMES and the COLORS, meaning that e.g. John is associated to both number 1 and color blue.

I'd like to merge df1 and df2 on the basis of the unique numbers, to get df_final:

df_final

UNIQUE NUMBER   NAME    COLOR
1               John    blue
2               Sarah   NaN
3               Luke    red
4               Bob     NaN
5               Annah   purple

Given that the two DataFrames have different lengths, the merge function doesn't seem to be applicable. Is it possible to create a DataFrame like this?

afr100
  • 31
  • 2
  • A merge doesn't require at all the inputs to have the same shape. Just use `df1.merge(df2, on='UNIQUE NUMBER', how='left')` – mozway May 05 '23 at 09:59

0 Answers0