0

I am trying to left merge two DataFrames but I want to left_on the first non null variable. Similar to a coalesce in SQL. Is there a way to do that?

In my example below I am left merging on a variable called 'clean_email' but I need to do something like coalesce(clean_email,email)

df = df.merge(df2, how='left',left_on='clean_email',right_on='clean_email')
spak
  • 253
  • 1
  • 2
  • 12

1 Answers1

0

use pandas.Series.combine_first¶

Combine Series values, choosing the calling Series’s values first.

import pandas as pd
import numpy as np  

s1 = pd.Series([1, np.nan])
s2 = pd.Series([3, 4])
s1.combine_first(s2)
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63