R语言 如何在siuba中匹配列值和提取索引?

wz3gfoph  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(69)

目标和数据

我的目标是在给定的frame_id处的vehicle_id中查找preceding的值,并在名为preceding_vel的新列中提取v_vel的相应值。我想使用siuba python包来实现这个目的。以下是我的dataframe:

import pandas as pd
    
    df_mini_dict = {'vehicle_id': {884: 2, 885: 2, 886: 2, 14148: 44, 14149: 44, 14150: 44}, 
'frame_id': {884: 338, 885: 339, 886: 340, 14148: 338, 14149: 339, 14150: 340}, 
'preceding': {884: 44, 885: 44, 886: 44, 14148: 3355, 14149: 3355, 14150: 3355}, 
'v_vel': {884: 6.299857770322456, 885: 6.427411525504063, 886: 6.590098168958994, 14148: 7.22883474245701, 14149: 6.973590500351793, 14150: 6.727721962795176}}
    
    df_mini = pd.DataFrame.from_dict(df_mini_dict)

字符串

R工作方案

我可以通过使用以下代码来实现目标:

df_mini <- structure(list(vehicle_id = c(2L, 2L, 2L, 44L, 44L, 44L), 
                          frame_id = c(338L, 339L, 340L, 338L, 339L, 340L), 
                          preceding = c(44L, 44L, 44L, 3355L, 3355L, 3355L), 
                          v_vel = c(6.29985777032246, 6.42741152550406, 
                                    6.59009816895899, 7.22883474245701, 
                                    6.97359050035179, 6.72772196279518), 
                          preceding_vel = c(7.22883474245701, 6.97359050035179, 
                                            6.72772196279518, NA, NA, NA)), 
                     class = c("tbl_df", "tbl", "data.frame"), 
                     row.names = c(NA, -6L))

library(dplyr)

df_mini <- df_mini |> 
  dplyr::group_by(frame_id) |>  
  dplyr::mutate(preceding_vel = v_vel[match(preceding, vehicle_id)]) |> 
  dplyr::ungroup()

Python尝试

本质上,我试图在siuba中做dplyr正在做的事情,但似乎我需要使用index()来做match所做的事情。我尝试了以下失败:

def match(x, table):
  indicez = []
  for i in x:
    indicez.append(table.index(i))
  return indicez

from siuba import *

df_mini = (
    df_mini
    >> group_by(_.frame_id)  # grouping by frame id
    >> mutate(preceding_vel = _.v_vel[match(_.preceding, _.vehicle_id)])
)

TypeError: 'Symbolic' object is not iterable


请告诉我什么是定义match函数的最佳方法,或者使用其他方法来满足目标。谢谢

w8ntj3qf

w8ntj3qf1#

按要求使用siuba:
我们将修改match函数,如下所示:

from siuba import _, mutate, group_by
from siuba.siu import symbolic_dispatch
import pandas as pd

@symbolic_dispatch(cls = pd.Series)
def match_get(a, b, c):
    b_dict = {x: int(i) for i, x in enumerate(b)}
    return [None if (d:=b_dict.get(x, None)) is None else c[d] for x in a]    

(df_mini >> 
 group_by(_.frame_id) >> 
 mutate(preceding_val = match_get(_.preceding, _.vehicle_id, _.v_vel.values)))

       vehicle_id  frame_id  preceding     v_vel  preceding_val
884             2       338         44  6.299858       7.228835
885             2       339         44  6.427412       6.973591
886             2       340         44  6.590098       6.727722
14148          44       338       3355  7.228835            NaN
14149          44       339       3355  6.973591            NaN
14150          44       340       3355  6.727722            NaN

字符串

oalqel3c

oalqel3c2#

使用merge

df_mini.merge(df_mini.drop(columns='preceding') , 
              left_on = ['frame_id', 'preceding'], 
              right_on = ['frame_id', 'vehicle_id'], how = 'left', 
              suffixes=['', '_y'])
Out[17]: 
   vehicle_id  frame_id  preceding     v_vel  vehicle_id_y   v_vel_y
0           2       338         44  6.299858          44.0  7.228835
1           2       339         44  6.427412          44.0  6.973591
2           2       340         44  6.590098          44.0  6.727722
3          44       338       3355  7.228835           NaN       NaN
4          44       339       3355  6.973591           NaN       NaN
5          44       340       3355  6.727722           NaN       NaN

字符串
R中的逻辑:

df_mini %>%
   left_join(select(., !preceding),
             c(preceding = 'vehicle_id', frame_id = 'frame_id'), 
               suffix = c("", "_y"))

# A tibble: 6 × 5
  vehicle_id frame_id preceding v_vel v_vel_y
       <int>    <int>     <int> <dbl>   <dbl>
1          2      338        44  6.30    7.23
2          2      339        44  6.43    6.97
3          2      340        44  6.59    6.73
4         44      338      3355  7.23   NA   
5         44      339      3355  6.97   NA   
6         44      340      3355  6.73   NA

相关问题