Convert Pandas Column to DateTime

前端 未结 5 2089
情话喂你
情话喂你 2020-11-21 11:38

I have one field in a pandas DataFrame that was imported as string format. It should be a datetime variable. How do I convert it to a datetime column and then filter based

5条回答
  •  后悔当初
    2020-11-21 11:54

    You can use the DataFrame method .apply() to operate on the values in Mycol:

    >>> df = pd.DataFrame(['05SEP2014:00:00:00.000'],columns=['Mycol'])
    >>> df
                        Mycol
    0  05SEP2014:00:00:00.000
    >>> import datetime as dt
    >>> df['Mycol'] = df['Mycol'].apply(lambda x: 
                                        dt.datetime.strptime(x,'%d%b%Y:%H:%M:%S.%f'))
    >>> df
           Mycol
    0 2014-09-05
    

提交回复
热议问题