Skip to content
Advertisement

Need help searching a pandas dataframe for a substring from another column

I have a dataframe (called combos) that looks like this (where player_title is an index column, and Team , Opp are regular column headers )

Player_Title Team Opp
QB Kirk Cousins MIN @ HOU
WR Adam Thielen MIN @ HOU
WR Justin Jefferson MIN @ HOU
RB Alvin Kamara NO @ DET
RB Myles Gaskin MIA vs SEA
WR Brandin Cooks HOU vs MIN
TE Logan Thomas WAS vs BAL
RB Kenyan Drake ARI @ CAR
DST Vikings MIN @ HOU

I am trying to write a conditional statement that sees whether or not the “Team” for the DST row appears anywhere in the Opp column. If it does, return true. In this example it should return true because the Opp of WR Brandon Cooks is Min.

I used combo[‘Team’][-1] to find the value of Team, and I used combo[‘Opp’][:-1] to find the list of Opponents I am trying to search through for Min. Then plugged into a lambda function that didnt find the matching substring. Ideally this would return true/false so that I can use it in an if/else statement, but havent figured out how to do that. combo[‘C’] = combo.apply(lambda x: x[‘Team’][-1] in x[‘Team’][:-1], axis=1)

Advertisement

Answer

What about:

combo['Team'].iloc[-1] in combo['Opp'][:-1].str[-3:].values
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement