1

我想从 a 中读取两列 S1_max 和 S2_max dataframe data。无论 S1_max 列中的哪个值存在,我都想检查每个S1_max值是否都有相应的S2_max信号。如果是这样,我计算S1_maxS2_max信号之间的时间增量。然后,此结果datetime[64ns]在单独的 S2_max 列的索引处被索引,dict d然后附加到list delta_data. 如何将此结果添加到data相应datetime[64ns]索引处已存在的数据框中?

这是我的创作delta_data

#time between each S2 global maxima: 86 ns/samp freq 200 = 0.43 ns
#Checking that each S1 is succeeded by a corresponging S2 signal and calculating the time delta:
delta_data = []
diff_S1 = 0
diff_S2 = 0
i = 0
while((i + diff_S1 + 1 < len(peak_indexes_S1)) and (i + diff_S2<len(peak_indexes_S2))):
# Find next ppg peak after S1 peak
    while (df["S2"].index[peak_indexes_S2[i + diff_S2]] < df["S1"].index[peak_indexes_S1[i+diff_S1]]):
        diff_S2=diff_S2+1

    while (df["S1"].index[peak_indexes_S1[i+diff_S1+1]] < df["S2"].index[peak_indexes_S2[i + diff_S2]]):
        diff_S1=diff_S1+1

    i_peak_S2 = peak_indexes_S2[i + diff_S2]
    i_peak_S1 = peak_indexes_S1[i + diff_S1]

    d={}
    d["td"] = (df["S2"].index[i_peak_S2]-df["S1"].index[i_peak_S1]).microseconds
    d["time"] = df["S2"].index[i_peak_S2]
    PATdata.append(d)

    i = i + 1

time_delta=pd.DataFrame(delta_data)

delta_data打印出来:

         td                    time
0    355000 2019-08-07 13:06:31.010
1    355000 2019-08-07 13:06:31.850
2    355000 2019-08-07 13:06:32.695

这是我的data数据框:

                           l1        l2        l3        l4       S1       S2   S2_max   S1_max

2019-08-07 13:11:21.485  0.572720  0.353433  0.701320  1.418840  4.939690  2.858326  2.858326       NaN
2019-08-07 13:11:21.490  0.572807  0.353526  0.701593  1.419052  4.939804  2.854604       NaN  4.939804

此数据框由以下人员创建:

data = pd.read_csv('file.txt')
data.columns = ['l1','l2','l3','l4','S1','S2']
nbrMeasurments = sum(1 for line in open('file.txt'))
data.index = pd.date_range('2019-08-07 13:06:30'), periods=nbrMeasurments-1, freq="5L")

我试过DataFrame.combine_firstappend.

此外,尝试将另一个数据帧添加到data. 此数据帧在日期时间帧中没有 ms:

                     S3   S4 
Date                                       
2019-08-07 13:06:30         111          61
4

1 回答 1

0

据我所知,您正在尝试将另一列附加到现有的 DataFrame 中。

这里怎么做:

df1 = pd.DataFrame({'names':['bla', 'blah', 'blahh'], 'values':[1,2,3]})
df2_to_concat = pd.DataFrame({'put_me_as_a_new_column':['row1', 'row2', 'row3']})

pd.concat([df1.reset_index(drop=True), df2_to_concat.reset_index(drop=True)], axis=1)

reset_index(drop=True)确保您不会产生 NaN 或重复的索引列。

于 2019-08-14T12:26:00.390 回答