Home > other >  best way to pickup different pandas column value if current column value is blank
best way to pickup different pandas column value if current column value is blank

Time:01-27

I'm trying to write a logic that will pickup a different column value if the current value is blank. Here is what I have so far:

df['column1'] = df.apply(lambda x: x["column2"] if x["column1"].astype(str)=='' else x["column1"], axis=1)

Is there a more efficient way to test for blank/null?

CodePudding user response:

Just do ffill if values are nan (if not replace '' with np.nan)

df['column1'] =  df['column1'].ffill(df['column2'])
  • Related