[英]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.