How to convert 01:00:00 into integer in Python?
Question:
I need to convert a column of a dataframe that is in the format of HH:MM:SS as a string to an integer, for example: 01:00:00 to 01 or 1.
data = ['01:00:00','02:00:00','03:00:00','04:00:00']
df = pd.DataFrame(data, columns=['Numbers'])
I already tried the parameter
astype(int) but it does not accept the format 01:00:00
Answers:
df["Numbers"] = df["Numbers"].map(lambda x: int(x[:2]))
I need to convert a column of a dataframe that is in the format of HH:MM:SS as a string to an integer, for example: 01:00:00 to 01 or 1.
data = ['01:00:00','02:00:00','03:00:00','04:00:00']
df = pd.DataFrame(data, columns=['Numbers'])
I already tried the parameter
astype(int) but it does not accept the format 01:00:00
df["Numbers"] = df["Numbers"].map(lambda x: int(x[:2]))