0

I have a dataframe called 'df':

  Value   Num
0 alpha     5
1 bravo     6
2 charlie   7

And a Series called 'series_to_add':

  New Value
0 alpha     
1 bravo     
2 delta

How can I combine the unique values of the series into the existing dataframe to get something like this:

  Value   Num
0 alpha     5
1 bravo     6
2 charlie   7
3 delta     nan
thePandasFriend
  • 115
  • 2
  • 10

1 Answers1

1

We can to_frame

s=s.to_frame('Value')
s
   Value
0  alpha
1  bravo
2  delta

Then do groupby get the first

pd.concat([df,s]).groupby('Value',as_index=False).first()
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
3    delta  NaN

Or drop_duplicates

pd.concat([df,s]).drop_duplicates('Value')
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
2    delta  NaN

Or merge

df.merge(s,how='outer')
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
3    delta  NaN
BENY
  • 317,841
  • 20
  • 164
  • 234