How to remove the date information in a column, just keep time

后端 未结 6 764
情深已故
情深已故 2020-12-19 01:54

I am using pandas dataframe. there is a specific column has time information.

the raw data likes this:

5:15am
5:28am
6:15am

so I ne

6条回答
  •  醉酒成梦
    2020-12-19 02:15

    your_date_df.dt.time

    Lets say that your column with the date ans time is df['arrived_date']:

    0   2015-01-06 00:43:00
    1   2015-01-06 07:56:00
    2   2015-01-06 11:02:00
    3   2015-01-06 11:22:00
    4   2015-01-06 15:27:00
    Name: arrived_date, dtype: datetime64[ns]
    

    Whith pandas, you just need to do:

    df['arrived_time']=df['arrived_date'].dt.time 
    

    The new column df['arrived_time'] will look like this:

    0    00:43:00
    1    07:56:00
    2    11:02:00
    3    11:22:00
    4    15:27:00
    Name: arrived_time, dtype: object
    

    Observe that the new column, df['arrived_time'], is no longer a datetime64 type, the type of the column is just a pandas object

提交回复
热议问题