数据框1包含测量数据:
ms force ... ... ...
1 5 20
2 10 20
3 15 25
4 20 30
5 25 20
..... (~ 6000 lines)
数据框2包含“定位数据”
ms speed (m/s)
1 0 0.66
2 4500 0.66
3 8000 1.3
4 16000 3.0
5 20000 3.0
.....(~300 lines)
现在我想使用第二数据帧中的数据计算第一个数据帧的位置 在Excel中,我通过使用数组公式解决了该问题,但是现在我必须使用Python / Pandas,但是我找不到找到从数据框2中选择正确行的方法。
我的想法是做这样的事情:如果
最后,我要显示“强制<->方式”而不是“强制<->时间”图
谢谢你
================================================ ========================== 更新: 同时,我几乎可以解决我的问题。现在我的数据看起来像这样:
数据框2(速度数据):
pos v a t t-end t-start
0 -3.000 0.666667 0.000000 4.500000 4.500000 0.000000
1 0.000 0.666667 0.187037 0.071287 4.571287 4.500000
2 0.048 0.680000 0.650794 0.010244 4.581531 4.571287
3 0.055 0.686667 0.205432 0.064904 4.646435 4.581531
...
15 0.055 0.686667 0.5 0.064904 23.0 20.0
...
28 0.055 0.686667 0.6 0.064904 35.0 34.0
...
30 0.055 0.686667 0.9 0.064904 44.0 39.0
和数据框1(基于时间的测量):
Fx Fy Fz abs_t expected output ('a' from DF1)
0 -13.9 170.3 45.0 0.005 0.000000
1 -14.1 151.6 38.2 0.010 0.000000
...
200 -14.1 131.4 30.4 20.015 0.5
...
300 -14.3 111.9 21.1 34.01 0.6
...
400 -14.5 95.6 13.2 40.025
所以我想从DF1检查时间(abs_t)并在DF2中搜索折角'a' 这样的东西(伪代码):
if (DF1['t_abs'] between (DF2['t-start'], DF2['t-end']):
DF1['a'] = DF2['a']
我可以做两个for循环,但是看起来像是错误的方式,而且非常慢。
答案 0 :(得分:0)
我找到了一个很慢的解决方案,但至少没有起作用:(
df1['a'] = 0
for index, row in df2.iterrows():
start = row['t-start']
end = row ['t-end']
a = row ['a']
df1.loc[(df1['tabs']>start)&(df1['tabs']<end), 'a'] = a