简体   繁体   English

根据其他数据帧从数据帧中选择值

[英]Select value from dataframe based on other dataframe

i try to calculate the position of an object based on a timestamp.我尝试根据时间戳计算对象的位置。 For this I have two dataframes in pandas.为此,我在熊猫中有两个数据框。 One for the measurement data and one for the position.一份用于测量数据,一份用于位置。 All the movement is a straightforward acceleration.所有的运动都是直接的加速。

Dataframe 1 contains the measurement data:数据帧 1 包含测量数据:

        ms        force   ...    ...    ...
1      5           20
2      10          20
3      15          25
4      20          30
5      25          20
..... (~ 6000 lines)

Dataframe 2 contains "positioning data"数据框 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)

Now I want to calculate the position of the first dataframe with the data from secound dataframe In Excel I solved the problem by using an array formular but now I have to use Python/Pandas and I cant find a way how to select the correct row from dataframe 2.现在我想用来自第二个数据帧的数据计算第一个数据帧的位置在 Excel 中我通过使用数组公式解决了这个问题,但现在我必须使用 Python/Pandas,我找不到如何从中选择正确行的方法数据框 2。

My idea is to make something like this: if我的想法是做这样的事情:如果

In the end I want to display a graph "force <-> way" and not "force <-> time"最后我想显示一个图表“力 <-> 方式”而不是“力 <-> 时间”

Thank you in andvance谢谢你

========================================================================== Update: In the meantime I could almost solve my issue. ================================================== ======================== 更新:与此同时,我几乎可以解决我的问题。 Now my Data look like this:现在我的数据看起来像这样:

Dataframe 2 (Speed Data):数据帧 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

And Dataframe 1 (time based measurement):和数据帧 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

So i want to check the time(abs_t) from DF1 and search for the corract 'a' in DF2 So somthing like this (pseudo code):所以我想检查来自 DF1 的时间(abs_t)并在 DF2 中搜索正确的 'a' 所以像这样(伪代码):

if (DF1['t_abs'] between (DF2['t-start'], DF2['t-end']):
    DF1['a'] = DF2['a']

I could make two for loops but it looks like the wrong way and is very very slow.我可以做两个 for 循环,但它看起来像错误的方式并且非常非常慢。

I hope you understand my problem;我希望你明白我的问题; to provide a running sample is very hard.提供运行样本非常困难。 In Excel I did like this:在 Excel 中,我是这样做的: 解决方案 数组公式

I found a very slow solution but atleast its working :(我找到了一个非常缓慢的解决方案,但至少它的工作原理:(

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM