2017-07-27 82 views
1

我需要將兩個數據集合併到一個數據集中。如何合併具有不同時間戳的兩隻熊貓

我有兩個生成的數據幀 - 熊貓,一個每秒〜數據採樣,另一個每〜120秒採樣一次數據。

我該如何合併這兩個,由應用程序的區間管轄。 120秒產生任務。

我目前已經將每120'樣本集從快速生成的1秒中抽出。數據集。這些不準確,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秒intevals

    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 

設置的數據的時間戳是不相同的,並且可以包含一個位的抖動。

我想數據列結合,因此數據(轉矩(Nm),速度[1/s]時,FFT ISO,FFTe:FO)相同120秒間隔內發生的結合。

也許我應該定義一個120秒的「參考區間」,並將數據放入這些相同大小的插槽中。

一個假設它可以使用pd.concatpd.append做,但我還沒有完全想通

任何幫助是如何理解

+1

你看着像[這一個](https://stackoverflow.com/questions/34880539任何疑問/熊貓-合併基礎上,一個時間戳,其中-DO-不匹配,完全一致)? – cmaher

回答

4

使用resample/mean方法將指標歸雙方通過取均值具有頻率120S在每個120秒的時間段內的所有值。

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 
+0

感謝您的迅速和準確的迴應;-) – opprud

相關問題