【问题标题】:How to merge two pandas with differing timestamps如何合并两个具有不同时间戳的熊猫
【发布时间】:2017-07-27 12:03:22
【问题描述】:

我需要将两个数据集组合成一个数据集。

我有两个生成的数据帧 - 熊猫,一个每隔 ~ 秒采样一次数据,另一个每隔 ~120 秒采样一次数据。

如何合并这两者,由应用程序的间隔控制。 120 秒生成任务。

我目前从快速生成的 1 秒中提取了每 120 个样本集。数据集。这些不准确,1 秒并且包含一点抖动。

                     Time  Torque [Nm]  Speed [1/s]
54240 2017-04-05 21:21:21       938.00       3000.0
54252 2017-04-05 21:23:23       936.25       3000.0
54264 2017-04-05 21:25:24       948.50       3000.0
54276 2017-04-05 21:27:26       948.50       3000.0
54288 2017-04-05 21:29:28       936.25       3000.0
54300 2017-04-05 21:31:29       952.00       3000.0
54312 2017-04-05 21:33:31       945.00       3000.0
54324 2017-04-05 21:35:33       927.50       3000.0

同样我有 120 秒间隔的数据集

                   Time   FFT ISO   FFTe: FO
0   2017-04-05 21:26:08   20.5754  16.377570
1   2017-04-05 21:28:08  106.1549  32.836566
2   2017-04-05 21:30:07   16.2735  19.308864
3   2017-04-05 21:32:08   24.2232  42.766070
4   2017-04-05 21:34:08   35.5723  64.152879
5   2017-04-05 21:36:08    3.7364  29.323316
6   2017-04-05 21:38:08   21.8207  17.796711
7   2017-04-05 21:40:08    9.9334  49.642802

时间戳不相同,可能包含一些抖动。

我想合并数据列,因此同一 120 秒间隔内发生的数据(扭矩 [Nm]、速度 [1/s]、FFT ISO、FFTe: FO)结合起来。

也许我应该定义一个 120 秒的“参考间隔”,并将数据放入这些大小相同的插槽中。

假设可以使用pd.concatpd.append 来完成,但我还不太清楚如何

感谢任何帮助

【问题讨论】:

  • 你有没有看过this one之类的问题?

标签: python pandas datetime append concat


【解决方案1】:

使用 resample/mean 方法通过取每 120 秒周期内所有值的平均值来标准化索引以使它们都具有频率 120S

resampled1 = df1.resample('120S').mean()
resampled2 = df2.resample('120S').mean()
result = resampled1.join(resampled2)

例如,

import numpy as np
import pandas as pd
np.random.seed(2017)

def make_index(N, freq):
    index = pd.date_range('2000-1-1', periods=N, freq=freq).view('i8')
    index = (np.sort(index + np.random.uniform(0, np.diff(index).mean(), size=N).astype(int))
             .view('datetime64[ns]'))
    return index

N = 100
sec_index = make_index(120*N, 'S')
sec120_index = make_index(N, '120S')

df1 = pd.DataFrame({'Torque': np.random.random(120*N),
                    'Speed': np.random.random(120*N),
                    'Time': sec_index})

df2 = pd.DataFrame({'FFT ISO': np.random.random(N),
                    'FFTe: FO': np.random.random(N),
                    'Time': sec120_index})

df1 = df1.set_index('Time')
df2 = df2.set_index('Time')

resampled1 = df1.resample('120S').mean()
resampled2 = df2.resample('120S').mean()
result = resampled1.join(resampled2)
print(result.head())

产量

                        Speed    Torque   FFT ISO  FFTe: FO
Time                                                       
2000-01-01 00:00:00  0.482262  0.470523  0.435150  0.289036
2000-01-01 00:02:00  0.501221  0.476776  0.005576  0.284386
2000-01-01 00:04:00  0.491305  0.459710  0.249217  0.253787
2000-01-01 00:06:00  0.486900  0.498921  0.391429  0.854698
2000-01-01 00:08:00  0.485611  0.517818  0.071058  0.552727

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 2019-07-16
    • 2021-09-05
    • 2020-08-06
    • 2021-01-09
    • 2016-01-09
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多