简体   繁体   中英

Replacing blank rows in a column with the value of another column

I have a dataframe with a date column but some values are blank or NaN, I am trying to replace all these values with a date from another column. I am trying the solution below but its not working, can someone please help

df['Date'] = df['Date'].replace(np.nan, df['Date2'])

some values are blank or NaN

You can first replace empty strings with NaN with replace then use fillna .

Try this:

df['Date'] = df['Date'].replace("", np.NaN).fillna(df['Date2'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM