我有三个完全不同的传感器源作为CSV文件的时间序列数据,并希望将它们组合成一个大CSV文件。
我已经设法用numpy的genfromtxt把它们读入numpy,但是我不知道从这里做什么。
基本上,我有这样的东西:
表1:

timestamp    val_a   val_b   val_c

表2:
timestamp    val_d   val_e   val_f   val_g

表3:
timestamp    val_h   val_i

所有时间戳都是UNIX毫秒时间戳,如NUMPY.UIT64。
我想要的是:
timestamp    val_a   val_b   val_c   val_d   val_e   val_f   val_g   val_h   val_i

…所有数据都由时间戳组合并排序。三个表中的每一个都已按时间戳排序。
由于数据来自不同的来源,不能保证表1的时间戳也将在表2或表3中,反之亦然。在这种情况下,空值应该标记为N/A。
到目前为止,我已经尝试使用pandas来转换数据,如下所示:
df_sensor1 = pd.DataFrame(numpy_arr_sens1)
df_sensor2 = pd.DataFrame(numpy_arr_sens2)
df_sensor3 = pd.DataFrame(numpy_arr_sens3)

然后尝试使用PANDAS.DATAFRAMI.合并,但我很确定,这对我现在要做的事情来说是行不通的。有人能指点我正确的方向吗?

最佳答案

我认为你可以
timestamp定义为每个indexDataFrame(使用set_index
使用join将它们与'outer'方法合并
选项将timestamp转换为datetime
这是它的样子。

# generating some test data
timestamp = [1440540000, 1450540000]
df1 = pd.DataFrame(
    {'timestamp': timestamp, 'a': ['val_a', 'val2_a'], 'b': ['val_b', 'val2_b'], 'c': ['val_c', 'val2_c']})
# building a different index
timestamp = timestamp * np.random.randn(abs(1))
df2 = pd.DataFrame(
    {'timestamp': timestamp, 'd': ['val_d', 'val2_d'], 'e': ['val_e', 'val2_e'], 'f': ['val_f', 'val2_f'],
     'g': ['val_g', 'val2_g']}, index=index)
# keeping a value in common with the first index
timestamp = [1440540000, 1450560000]
df3 = pd.DataFrame({'timestamp': timestamp, 'h': ['val_h', 'val2_h'], 'i': ['val_i', 'val2_i']}, index=index)

# Setting the timestamp as the index
df1.set_index('timestamp', inplace=True)
df2.set_index('timestamp', inplace=True)
df3.set_index('timestamp', inplace=True)

# You can convert timestamps to dates but it's not mandatory I think
df1.index = pd.to_datetime(df1.index, unit='s')
df2.index = pd.to_datetime(df2.index, unit='s')
df3.index = pd.to_datetime(df3.index, unit='s')

# Just perform a join and that's it
result = df1.join(df2, how='outer').join(df3, how='outer')
result

python - 使用numpy/pandas按时间戳合并时间序列数据-LMLPHP

关于python - 使用numpy/pandas按时间戳合并时间序列数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32215024/

10-12 17:02
查看更多