Remove dtype datetime NaT

前端 未结 4 1922
面向向阳花
面向向阳花 2020-12-28 16:53

I am preparing a pandas df for output, and would like to remove the NaN and NaT in the table, and leave those table locations blank. An example would be

myd         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 17:58

    I had the same issue: This does it all in place using pandas apply function. Should be the fastest method.

    import pandas as pd
    df['timestamp'] = df['timestamp'].apply(lambda x: x.strftime('%Y-%m-%d')if not pd.isnull(x) else '')
    

    if your timestamp field is not yet in datetime format then:

    import pandas as pd
    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda x: x.strftime('%Y-%m-%d')if not pd.isnull(x) else '')
    

提交回复
热议问题