英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论