1

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)

2
  • What's the expected output? Commented Aug 31, 2021 at 4:49
  • The expected output should be True based on the sample data combo['Team'][-1] = 'MIN' >>> combo['Opp'][:-1].str[-3:] Player_Title WR Brandin Cooks MIN I am trying to use it in an if/else statement and mozway's answer almost works except it returns False even though MIN exists in the Opp field. Commented Aug 31, 2021 at 11:57

1 Answer 1

2

What about:

combo['Team'].iloc[-1] in combo['Opp'][:-1].str[-3:].values
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but it the return statement comes out false instead of true, where I expected True for the sample dataset. I want to add that I also tried removing the vs and @ symbols to see if they were causing the misatch but that wasnt the case.
perfect thank you! A side note, I was testing while you were doing that and this also worked: combo['Team'][-1] in str(combo['Opp'][:-1].str[-3:])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.