如何使用python将pandas数据框中的日期格式列修改为int

具有列名称和exm_date的df

name          exm_date                             
tom       2019-03-05 11:48:03.166              
mark      2018-03-05 11:48:03.166                   
matt      2020-08-05 11:48:03.166                
rob       2020-06-05 11:48:03.166              
chuck     2020-02-05 11:48:03.166               
tom       2020-03-05 11:48:03.166              
matt      2020-02-05 11:48:03.166                 
chuck     2020-06-05 11:48:03.166                    

how to convert the date format in exm_date into a format removing the time stamp and exluding the -

预期输出

name          exm_date                         code_date    
tom       2019-03-05 11:48:03.166              20190305
mark      2018-03-05 11:48:03.166              20180305     
matt      2020-08-05 11:48:03.166              20200805  
rob       2020-06-05 11:48:03.166              20200605
chuck     2020-02-05 11:48:03.166              20200205 
tom       2020-03-05 11:48:03.166              20200305
matt      2020-02-05 11:48:03.166              20200305    
chuck     2020-06-05 11:48:03.166              20200305       
评论
  • zsed
    zsed 回复

    请尝试以下代码:

    df['code_date'] = df['exm_date'].apply(lambda x: x.strftime("%Y%d%m"))
    
  • saut
    saut 回复

    您可以尝试以下方法:

    import pandas as pd
    df['code_date'] = pd.to_datetime(df['exm_date']).dt.strftime('%Y%m%d')