[英]Extracting the hour from a time column in pandas
Suppose I have the following dataset:假设我有以下数据集:
How would I create a new column, to be the hour of the time?我将如何创建一个新列,作为时间的小时?
For example, the code below works for individual times, but I haven't been able to generalise it for a column in pandas.例如,下面的代码适用于个别时间,但我无法将其概括为 Pandas 中的一列。
t = datetime.strptime('9:33:07','%H:%M:%S')
print(t.hour)
Use to_datetime
to datetimes with dt.hour
:使用
to_datetime
到日期时间与dt.hour
:
df = pd.DataFrame({'TIME':['9:33:07','9:41:09']})
#should be slowier
#df['hour'] = pd.to_datetime(df['TIME']).dt.hour
df['hour'] = pd.to_datetime(df['TIME'], format='%H:%M:%S').dt.hour
print (df)
TIME hour
0 9:33:07 9
1 9:41:09 9
If want working with datetime
s in column TIME
is possible assign back:如果想要在列
TIME
使用datetime
s,则可以分配回:
df['TIME'] = pd.to_datetime(df['TIME'], format='%H:%M:%S')
df['hour'] = df['TIME'].dt.hour
print (df)
TIME hour
0 1900-01-01 09:33:07 9
1 1900-01-01 09:41:09 9
My suggestion:我的建议:
df = pd.DataFrame({'TIME':['9:33:07','9:41:09']})
df['hour']= df.TIME.str.extract("(^\d+):", expand=False)
You can use extract() twice to feature out the 'hour' column您可以使用 extract() 两次来突出“小时”列
df['hour'] = df. TIME. str. extract("(\d+:)")
df['hour'] = df. hour. str. extract("(\d+)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.