谢谢你看这个..。
需要降低IoT传感器的数据时间戳和合并精度。
我有两个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.4CSV-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我们不知道如何根据日期时间将两个csv文件连接、合并、附加到主数据中。我们想要的是:
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未来我们还会增加一些传感器.光,CO2,所以几乎每一秒都会有一列包含数据的列。
我们还希望对频率不同的传感器如何能够收集数据及其准确性进行一些分析,从而使用主数据。
你们都很棒!谢谢你的帮助。
发布于 2017-06-15 14:10:37
温度(温度)数据:
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 dataframe看起来如下:
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万岁!
发布于 2017-06-15 14:06:30
我认为解决问题的方法是使用pd.join()。
df_joined = ugt.join(ugh, how='outer')
temperature humidity
2017-06-13 22:06:03 82.0 63.0
2017-06-13 22:06:20 82.4 NaN
2017-06-13 22:06:21 NaN 63.0
2017-06-13 22:06:37 82.4 NaN
2017-06-13 22:06:38 NaN 63.0
2017-06-13 22:06:57 82.4 63.0连接之后,通过遍历已连接的dataframe并使用索引对每一行进行遍历,填充主数据each:
for index, row in df_joined.iterrows():
df_master.loc[index,'humidity'] = row['humidity']
df_master.loc[index,'temperature'] = row['temperature']我没有输出,因为我没有构建一个主数据格式,但是它应该能工作
https://stackoverflow.com/questions/44568806
复制相似问题