简体   繁体   English

搜索数据框中多列中的最后一次出现

[英]Search for the last occurence in multiple columns in a dataframe

Suppose i have a large dataframe similar to the structure below 假设我有一个类似于下面结构的大型数据帧

 home| away|  home_score| away_score
    A|    B|           1|          0
    B|    C|           1|          1
    C|    A|           1|          0

I want to find the last score regardless of home / away. 我想找到最后的分数,无论家里/外。 For example, last score of team A, B and C are 0, 1 and 1 respectively and fill back to the original dataframe: 例如,团队A,B和C的最后得分分别为0,1和1,并填充回原始数据帧:

 home| away|  home_score| away_score| last_score_home| last_score_away|
    A|    B|           1|          0|                |                |
    B|    C|           1|          1|               0|                |
    C|    A|           1|          0|               1|               1|
 ...

I have tried groupby and shift but I am not sure how to combine the home / away results. 我尝试过groupby和shift,但我不确定如何结合主/结果。

You can try something as this. 你可以试试这个。 1) make all column names splittable by adding suffix to the first two columns names; 1)通过在前两列名称中添加后缀,使所有列名可拆分; 2) split the column headers and transform it to multi index; 2)拆分列标题并将其转换为多索引; 3) melt table to long format with stack , group by the teams and get the latest score: 3)融合表到长格式与stack ,由团队分组并获得最新分数:

df.columns = df.columns.str.replace("^([^_]+)$", "\\1_team").str.split("_", expand=True)
df.stack(level=0).groupby("team").tail(1)

#         score   team
#1  home      1      B
#2  away      0      A
#   home      1      C

Update : 更新

To merge it back to the original data frame, you can use join : 要将其合并回原始数据框,您可以使用join

df.columns = df.columns.str.replace("^([^_]+)$", "\\1_team").str.split("_", expand=True)
df1 = df.stack(level=0).groupby("team").tail(1)   

# join the result back to the original transformed data frame 
df2 = df.stack(level=0).join(df1.score, rsuffix = "_last").unstack(level=1)
df2.columns = [x + "_" + y for x, y in df2.columns]
df2

在此输入图像描述

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

相关问题 python dataframe 中最后出现的逗号 - Last occurence of comma in python dataframe 自上一次在Pandas DataFrame中发生以来还没有几天? - Getting days since last occurence in Pandas DataFrame? DataFrame groupby 2 列 und 计数出现在第三 - DataFrame groupby 2 columns und count occurence in third 在Python中查找字符串中多个字符的最后一次出现 - find last occurence of multiple characters in a string in Python 如何在Dataframe中最后一次出现字符后删除所有内容? - How to remove everything after the last occurence of a character in a Dataframe? 如何根据 pandas 列中最后一次出现的字符串提取 dataframe 的子集? - How to extract a subset of dataframe based on the last occurence of a string in a column in pandas? 在Panda / Python中最后一次出现值时执行复杂的数据框过滤请求[编辑] - complex dataframe filtering request on the last occurence of a value in Panda/Python [EDIT] 使用SSH,在文件中搜索字符串,并在最后一次出现时返回其旁边的值 - Using SSH, search a file for a string and return the value next to it in the last occurence Python循环在dataframe的所有列中搜索多组关键字 - Python loop to search multiple sets of keywords in all columns of dataframe 在pandas DataFrame中搜索列 - Search for columns in a pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM