Home > Enterprise >  Dataframe fillna() for datetime based on another column
Dataframe fillna() for datetime based on another column

Time:04-14

I'm trying to use .fillna to replace NaT data from another column, follows example:

id data1 data2
1 2021-02-12 16:58:38.570 2021-02-12 17:30:55.000
2 NaT 2021-04-20 14:32:46.000

Expected:

id data1 data2
1 2021-02-12 16:58:38.570 2021-02-12 17:30:55.000
2 2021-04-20 14:32:46.000 2021-04-20 14:32:46.000

I tried

df['data1'].fillna(df['data2'], inplace=True) 

and nothing

Also tried

df['data1'] = df_final['data1'].replace('nan', np.nan).fillna(df['data2'], inplace=True) 

but converted all of the data1 column to None

Any clues?

CodePudding user response:

It seems it's not actually NaT but a string 'NaT'. So you could replace it with NaN, then could try bfill on axis:

df = df.replace('NaT', pd.NA).bfill(axis=1)

Output:

  id                    data1                    data2
0  1  2021-02-12 16:58:38.570  2021-02-12 17:30:55.000
1  2  2021-04-20 14:32:46.000  2021-04-20 14:32:46.000

CodePudding user response:

You can use the below function which will gives you better customization

def replaceNone(df):
    for index, row in df.iterrows():
        if row["data1"]==None:
            df.loc[index,"data1"] = row["data2"]
    return df
  • Related