如何将包含数据和datetime64 [ns]的列表与带有datetime64 [ns]索引的熊猫数据框合并

时间:2019-08-14 12:02:51

标签: python pandas dataframe datetime64

我想从dataframe data读取两列S1_max和S2_max。无论S1_max列中存在什么值,我都想检查每个S1_max是否被相应的S2_max信号所代替。如果是这样,我将计算S1_maxS2_max信号之间的时间增量。然后,在单独的datetime[64ns] dict的S2_max列的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

1 个答案:

答案 0 :(得分: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或重复的索引列。