[英]Python/Pandas — convert day and hour columns into index of hour
I have a Data Frame that looks like this: 我有一个看起来像这样的数据框:
df
Date Hr CO2_resp
0 5/1/02 600 0.000889
1 5/2/02 600 0.000984
2 5/4/02 900 0.000912
How would I go about creating a column Ind
that represents a number index of hours elapsed since midnight 5/1/02? 我将如何创建一个列Ind
来表示自午夜5/1/02以来经过的小时数的索引? Such that the column would read 这样,该列将显示为
df
Date Hr Ind CO2_resp
0 5/1/02 600 6 0.000889
1 5/2/02 600 30 0.000984
2 5/4/02 800 80 0.000912
Thanks. 谢谢。
Assuming that the Date
is a string, and Hr
is an integer, you could apply a function to parse the Date
, get the hours (days * 24) from the timedelta
with your reference date, and add the hours. 假设Date
是一个字符串,并且Hr
是一个整数,则可以应用一个函数来解析Date
,从timedelta
带有您的参考日期的小时数(天* 24),然后添加小时数。
Something like this - 像这样-
df.apply(lambda x:
(datetime.datetime.strptime(x['Date'], '%m/%d/%y')
- datetime.datetime.strptime('5/1/02', '%m/%d/%y')).days
* 24 + x['Hr'] / 100,
axis=1)
You can use to_datetime
with to_timedelta
. 您可以将to_datetime
与to_timedelta
to_datetime
使用。 Then convert timedelta
to hours
by np.timedelta64(1, 'h')
and last if type
of output is always int
, cast by astype
: 然后通过np.timedelta64(1, 'h')
将timedelta
转换为hours
,如果输出的type
始终是int
,则通过astype
:
#convert column Date to datetime
df['Date'] = pd.to_datetime(df.Date)
df['Ind'] = ((df.Date
- pd.to_datetime('2002-05-01')
+ pd.to_timedelta(df.Hr / 100, unit='h')) / np.timedelta64(1, 'h')).astype(int)
print (df)
Date Hr CO2_resp ind
0 2002-05-01 600 0.000889 6
1 2002-05-02 600 0.000984 30
2 2002-05-04 900 0.000912 81
If not dividing by 100
column Hr
, output is different: 如果不除以100
列Hr
,则输出将不同:
df['Ind'] = ((df.Date
- pd.to_datetime('2002-05-01')
+ pd.to_timedelta(df.Hr,unit='h')) / np.timedelta64(1, 'h')).astype(int)
print (df)
Date Hr CO2_resp Ind
0 2002-05-01 600 0.000889 600
1 2002-05-02 600 0.000984 624
2 2002-05-04 900 0.000912 972
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.