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


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?

Asked By: Mike Mann



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

df['column1'] =  df['column1'].ffill(df['column2'])
Answered By: anky
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.