Convert string date to a different format in pandas dataframe

后端 未结 2 1196
忘掉有多难
忘掉有多难 2021-01-21 16:51

I have been looking for this answer in the community so far, could not have.

I have a dataframe in python 3.5.1 that contains a column with dates in string imported from

2条回答
  •  抹茶落季
    2021-01-21 17:14

    For most common date and datetime formats, pandas .to_datetime function can parse them without we providing format. For example:

    df.TimeStamp.apply(lambda x: pd.to_datetime(x))

    And in the example given from the question,

    df['TimeStamp'] = pd.to_datetime(df['TimeStamp']).dt.strftime('%m/%d/%Y %H:%M:%S')

    will give us the same result.

    Using .apply will be efficient if you have multiple columns.

    Of course, providing the parsing format is necessary for many situations. For a full list of formats, please see https://docs.python.org/3/library/datetime.html.

提交回复
热议问题