如何在Pandas DF中的一列上进行迭代以检查特定值并在同一行中但从另一列返回一个值?

df包含基于事件的足球数据。因此,我要进行的操作是遍历action_name列并查找值“目标”(如果找到),然后我想存储与“目标”操作名称在同一行中的多个其他列的值。以下是数据结构的示例。

data structure

因此,在这种情况下,我想为DF中的每个目标返回pos_dest_x,pos_dest_y和possesion_number并将它们存储为新的DF。

评论
  • W@无情
    W@无情 回复

    这应该可以解决问题:

    df.loc[df["action_name"]=="Goal", ["pos_dest_x", "pos_dest_y", "possesion_number"]]
    
  • R幻影
    R幻影 回复

    Assuming your df has the name actions_df, you should be able to run something like:

    goals_df = actions_df[actions_df['action_name']=="Goal"][['pos_dest_x','pos_dest_y','possession_number']]
    

    Which selects rows from the actions_df where action_name is equal to goal, and the filters the columns down to the ones you want to see.