teratoulis
teratoulis

Reputation: 67

Change the timestamp format in a dataframe

enter image description hereI have a dataframe with timestamp like this

enter image description here

I would like to use random forest and for this reason I would like to convert in a different format like this:

hour minute

enter image description here

is it possible?

thanks in advance

Upvotes: 0

Views: 32

Answers (1)

tavito
tavito

Reputation: 190

You should try using strftime:

df['timestamp'] = df['timestamp'].apply(lambda x: x.strftime('%H %M'))

If you want the result in different columns, could you try adding the following code:

df[['hour', 'minute']] = df['timestamp'].str.extract('(\d\d) (\d\d)')

Hope this can help you!

Upvotes: 1

Related Questions