2017-06-15 46 views
3

谢谢你看这....合并数据帧后,通过日期后,持续降低

需要降低物联网传感器数据时间戳和合并的精度。

我有以下数据

2个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)) 

产生以下dataframes:

     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 

我们无法弄清楚如何使用熊猫CONCAT,合并,两家的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 

有额外的传感器,我们将在未来....光,二氧化碳增加,所以几乎每一秒钟都会最终不得不在它的数据列。

我们还希望对各种传感器能够收集数据的频率及其精度进行一些分析,从而使用主数据帧。

你们都摇滚!谢谢你的帮助。

+0

您是否需要创建主数据框?看起来似乎很稀疏,听起来好像会有很多循环来填充它(尽管可能有一些熊猫的方法来解决它)。 –

+0

我们可以将两个csv文件合并为只包含条目(跳过没有任何传感器值的秒),但即使是空的,每秒钟的主DF也可以让我们更容易地分析传感器能够收集数据,以及它可以做到多准确。欢迎您的想法 –

+0

好吧我提出了一个主数据框的解决方案,让我知道它是否工作。 –

回答

0

我相信你的问题的解决方案是使用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 

后,通过加入该数据帧的循环,并使用索引来assing每一行加入填充主数据框:

for index, row in df_joined.iterrows(): 
    df_master.loc[index,'humidity'] = row['humidity'] 
    df_master.loc[index,'temperature'] = row['temperature'] 

我没有输出,因为我没有建主数据帧,但它应该工作

+0

这个答案的第一部分是非常有帮助的。这个答案的第二部分超时了,因为它们有90,000行要更新。 –

+0

是的,我不会推荐使用主数据框。我会解决它,但很难说,因为我不知道你的具体要求。就像你在那段时间找不到条目,​​那么在进行分析时就是NaN。 –

1

温度(温度)数据帧:

    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 

现在的临时数据框的样子:

     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 

Reindex temp,根据需要替换日期:

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 

万岁!