Home > Mobile >  clean data and convert it to number-pandas
clean data and convert it to number-pandas

Time:12-31

I have a column in the table with a date and time.

I just want to take the hour out and convert it into a number.

For example: if registered 00:01 I want to make it 00. If registered: 18:20 I want to make it 18 etc.

I could not figure out how to do this with pandas.

I am attaching a picture where you can see what the column looks like.

I would be very happy to help (:

image

CodePudding user response:

You can try this:

import pandas as pd

df['hour'] = pd.to_datetime(df['Date']).dt.hour

Please let me know if I misunderstood your question!

CodePudding user response:

You can convert to datetime, get the hour, then use zfill to get the zero padding you want.

import pandas as pd
df = pd.DataFrame({'Date':['01/01/2004 00:01','06/20/2004 11:00:00 AM','12/30/2004 08:00:00 PM']})

pd.to_datetime(df['Date']).dt.hour.astype(str).str.zfill(2)

Output

0    00
1    11
2    20
  • Related