【发布时间】:2020-01-17 23:50:08
【问题描述】:
注意:这个问题与combining three different timestamp dataframes using duration match这里的回答问题有些相似
我有两个主数据帧和一个从数据帧。两个主数据帧数据每 30 分钟出现一次。我将三个数据帧与主数据帧结合起来作为参考,并匹配来自从属的时间戳,如下所示。在特定会话期间获取的两个主人的数据应该出现在一行中。
我的意见是
mas_df1 =
index S1
2019-01-09 13:20:17 2202.517620
2019-01-09 14:00:17 2392.173558
mas_df2 =
index S2
2019-01-09 13:24:32 2134.791454
2019-01-09 14:04:32 1958.719125
mas_list = [mas_df1,mas_df2]
slv_df =
index POA
2019-01-09 13:20:00 752.743700
2019-01-09 13:20:17 742.961815
2019-01-09 13:24:32 697.267647
2019-01-09 13:24:48 699.418420
2019-01-09 14:00:00 778.720800
2019-01-09 14:00:17 791.852790
2019-01-09 14:04:32 691.605547
2019-01-09 14:04:48 688.313520
合并后的数据帧应该有两个master的时间戳和完整的数据。但是,只有从属 df 的数据应该以匹配的时间戳附加到它。
我目前的代码 如下所示。
aux = []
for i in range(0,len(mas_list),1):
s1=slv_df['POA'].reindex(mas_list[i].index,method='nearest').add_prefix(mas_list[i].columns[0])
if i==0:
aux.append(s1.join(mas_list[i]))
else:
aux.append(s1.join(mas_list[i]).reindex(aux.index,method='nearest'))
cmb_df = pd.concat(aux,axis=1)
我现在的输出是:
raise ValueError("cannot reindex a non-unique index "
ValueError: cannot reindex a non-unique index with a method or limit
我的预期输出是:
cmd_df =
index S1 S1POA S2 S2POA
2019-01-09 13:20:17 2202.517620 742.961815 2134.791454 697.267647
2019-01-09 14:00:17 2392.173558 791.852790 1958.719125 691.605547
有什么改进我的代码的建议吗?
【问题讨论】:
-
我会使用 df.merge() 而不是 concat()。