我的pandas数据框包含两列(系列对象)。它们是具有数据类型序列的格式H:M:S。我想找出两个时间序列之间明显不同的元素,找出一天的长度。也就是说,
H2:M2:S2-H1:M1:S1
我是python的新手。有什么建议吗?
这是我的数据框:
df.head()
UNIXTime Data Time Radiation Temperature Pressure Humidity WindDirection(Degrees) Speed TimeSunRise TimeSunSet
0 1475229326 9/29/2016 12:00:00 AM 23:55:26 1.21 48 30.46 59 177.39 5.62 06:13:00 18:13:00
1 1475229023 9/29/2016 12:00:00 AM 23:50:23 1.21 48 30.46 58 176.78 3.37 06:13:00 18:13:00
2 1475228726 9/29/2016 12:00:00 AM 23:45:26 1.23 48 30.46 57 158.75 3.37 06:13:00 18:13:00
3 1475228421 9/29/2016 12:00:00 AM 23:40:21 1.21 48 30.46 60 137.71 3.37 06:13:00 18:13:00
4 1475228124 9/29/2016 12:00:00 AM 23:35:24 1.17 48 30.46 62 104.95 5.62 06:13:00 18:13:00
print(df.dtypes)
UNIXTime int64
Data object
Time object
Radiation float64
Temperature int64
Pressure float64
Humidity int64
WindDirection(Degrees) float64
Speed float64
TimeSunRise object
TimeSunSet object
dtype: object
df.TimeSunSet.__dict__
{'_cacher': ('TimeSunSet',
<weakref at 0x000000000B5C04F8; to 'DataFrame' at 0x000000000B556438>),
'_data': SingleBlockManager
Items: RangeIndex(start=0, stop=32686, step=1)
ObjectBlock: 32686 dtype: object,
'_index': RangeIndex(start=0, stop=32686, step=1),
'_item_cache': {},
'_name': 'TimeSunSet',
'_subtyp': 'series',
'is_copy': None}
我想找到TimeSunSet - TimeSunRise
之间的区别。这是最后一列与最后一列之间的差异。我在windows anaconda上使用python 3.5.1。
答案 0 :(得分:1)
I think you need convert columns to_timedelta
获取数据,然后减去:
df['difference'] = pd.to_timedelta(df['TimeSunSet']) - pd.to_timedelta(df['TimeSunRise'])
print (df)
UNIXTime Data Time Radiation Temperature \
0 1475229326 9/29/2016 12:00:00 AM 23:55:26 1.21 48
1 1475229023 9/29/2016 12:00:00 AM 23:50:23 1.21 48
2 1475228726 9/29/2016 12:00:00 AM 23:45:26 1.23 48
3 1475228421 9/29/2016 12:00:00 AM 23:40:21 1.21 48
4 1475228124 9/29/2016 12:00:00 AM 23:35:24 1.17 48
Pressure Humidity WindDirection(Degrees) Speed TimeSunRise TimeSunSet \
0 30.46 59 177.39 5.62 06:13:00 18:13:00
1 30.46 58 176.78 3.37 06:13:00 18:13:00
2 30.46 57 158.75 3.37 06:13:00 18:13:00
3 30.46 60 137.71 3.37 06:13:00 18:13:00
4 30.46 62 104.95 5.62 06:13:00 18:13:00
difference
0 12:00:00
1 12:00:00
2 12:00:00
3 12:00:00
4 12:00:00