谢谢你看这个。。。。
需要降低物联网传感器数据时间戳的精度并合并。
我有两个csv文件,包含以下数据
CSV-1号
datetime,temperature
2017-06-13 22:20:11.309,82.4
2017-06-13 22:19:54.004,82.4
2017-06-13 22:19:36.661,82.4
2017-06-13 22:19:19.359,82.4
CSV-2号
datetime,humidity
2017-06-13 22:07:30.723,63.0
2017-06-13 22:07:13.448,63.0
2017-06-13 22:06:56.115,63.0
2017-06-13 22:06:38.806,63.0
请注意,日期时间项是毫秒。我使用以下代码将精度降低到秒。
ugt = pd.read_csv('ugt.csv', parse_dates=True, index_col=0)
ugh = pd.read_csv('ugh.csv', parse_dates=True, index_col=0)
ugt.index = ugt.index.map(lambda x: x.replace(microsecond=0))
ugh.index = ugh.index.map(lambda x: x.replace(microsecond=0))
生成以下数据帧:
temperature
datetime
2017-06-13 22:06:57 82.4 <---
2017-06-13 22:06:37 82.4
2017-06-13 22:06:20 82.4
2017-06-13 22:06:03 82.0 <---
humidity
datetime
2017-06-13 22:06:57 63.0 <---
2017-06-13 22:06:38 63.0
2017-06-13 22:06:21 63.0
2017-06-13 22:06:03 63.0 <---
请注意,有些时间戳与第二个时间戳匹配(请参见然后,我们创建一个主数据帧,在从所有传感器收集数据的时间段内,该主数据帧每天的任何一秒都填充有行。
temperature humidity
2017-04-25 12:00:00 0 0
2017-04-25 12:00:01 0 0
2017-04-25 12:00:02 0 0
2017-04-25 12:00:03 0 0
2017-04-25 12:00:04 0 0
我们不知道如何使用pandas concat,合并,将两个csv文件附加到基于datetime的主数据帧中。我们需要的是:
temperature humidity
2017-04-25 12:00:00 0 0
2017-04-25 12:00:01 82.0 0
2017-04-25 12:00:02 0 44.0
2017-04-25 12:00:03 0 0
2017-04-25 12:00:04 82.0 44.0
2017-04-25 12:00:05 0 0
2017-04-25 12:00:06 82.0 0
2017-04-25 12:00:07 0 0
2017-04-25 12:00:08 82.0 44.0
我们将在未来增加更多的传感器。。。。光,二氧化碳,所以几乎每秒钟都会有一列数据。
我们还想对不同频率的传感器如何收集数据及其精度进行一些分析,从而使用主数据帧。
你们都摇滚!谢谢你的帮助。
最佳答案
温度数据框:
datetime temperature
0 2017-06-13 22:20:11.309 82.4
1 2017-06-13 22:19:54.004 82.4
2 2017-06-13 22:19:36.661 82.4
3 2017-06-13 22:19:19.359 82.4
潮湿数据框:
datetime humidity
0 2017-06-13 22:07:30.723 63.0
1 2017-06-13 22:07:13.448 63.0
2 2017-06-13 22:06:56.115 63.0
3 2017-06-13 22:06:38.806 63.0
temp.datetime = pd.to_datetime(temp.datetime) #convert to datetime dtype
temp.set_index('datetime', inplace=True) #make it the index
temp.index = temp.index.round('S') #and now round to the second
现在,temp数据帧看起来像:
temperature
datetime
2017-06-13 22:20:11 82.4
2017-06-13 22:19:54 82.4
2017-06-13 22:19:37 82.4
2017-06-13 22:19:19 82.4
对于潮湿的df,也要这样做:
humid.datetime = pd.to_datetime(humid.datetime)
humi.set_index('datetime', inplace=True)
humid.index = humid.index.round('S')
现在潮湿的是:
humidity
datetime
2017-06-13 22:07:31 63.0
2017-06-13 22:07:13 63.0
2017-06-13 22:06:56 63.0
2017-06-13 22:06:39 63.0
重新编制临时索引,根据需要替换日期:
temp = temp.reindex(pd.DatetimeIndex(start='2017-06-13 22:00', end='2017-06-13 22:20', freq='S'))
temp.head()
temperature
2017-06-13 22:00:00 NaN
2017-06-13 22:00:01 NaN
2017-06-13 22:00:02 NaN
2017-06-13 22:00:03 NaN
2017-06-13 22:00:04 NaN
现在左键连接:
out = pd.merge(temp, humid, left_index=True, right_index=True, how='left')
out.head():
temperature humidity
2017-06-13 22:00:00 NaN NaN
2017-06-13 22:00:01 NaN NaN
2017-06-13 22:00:02 NaN NaN
2017-06-13 22:00:03 NaN NaN
2017-06-13 22:00:04 NaN NaN
确保这确实有效:
out.loc['2017-06-13 22:07:31']
temperature humidity
2017-06-13 22:07:31 NaN 63.0
万岁!