如果另一列中存在字符串,则向前填充数据框列。

huangapple go评论67阅读模式
英文:

Forward fill dataframe column if string is present in another column

问题

    类型	                                                   匹配	         对决
  父级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA	NYY-vs-LAA
   子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA	NYY-vs-LAA
 子子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD	NYM-vs-LAD
 子子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD	NYM-vs-LAD
   测试	                              All_Star_Game_12252000	
   子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS	BAL-vs-BOS
  父级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS	BAL-vs-BOS
   子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL
  父级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL
 子子级	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL
英文:

I have a dataframe and I want to forward fill one of the columns but only if the string is present in one of the other columns.

       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA         NaN
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD         NaN
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS         NaN
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN

For example, if Matchup is present Match, I want to forward fill it so that the output looks like:

    Type	                                               Match	   Matchup
  Parent	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA	NYY-vs-LAA
   Child	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA	NYY-vs-LAA
SubChild	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD	NYM-vs-LAD
SubChild	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD	NYM-vs-LAD
    Test	                              All_Star_Game_12252000	
   Child	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS	BAL-vs-BOS
  Parent	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS	BAL-vs-BOS
   Child	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL
  Parent	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL
SubChild	ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL	TBR-vs-COL

答案1

得分: 0

窃取 @mozway 的评论

from io import StringIO

import pandas as pd

df = pd.read_csv(
    StringIO('''Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
'''),
    sep=r"\s+"
)

df.update(df.groupby("Match")["Matchup"].ffill())

print(df)
英文:

Stealing @mozway's comment

from io import StringIO

import pandas as pd


df = pd.read_csv(
    StringIO("""
       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA         NaN
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD         NaN
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS         NaN
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL         NaN
"""),
    sep="\s+"
)

df.update(df.groupby("Match")["Matchup"].ffill())

print(df)
       Type                                                 Match     Matchup
0    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
1     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYY-vs-LAA  NYY-vs-LAA
2  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
3  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_NYM-vs-LAD  NYM-vs-LAD
4      Test                                All_Star_Game_12252000         NaN
5     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
6    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_BAL-vs-BOS  BAL-vs-BOS
7     Child  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
8    Parent  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL
9  SubChild  ABC_12252000_NY_Leag_Natl_en-NY_RegSeason_TBR-vs-COL  TBR-vs-COL

References

huangapple
  • 本文由 发表于 2023年2月27日 02:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定