1

I have a dataframe of race results and I am trying to see if the winner of the race is from the same location as the race.

round_loc column:

0 Val d'Allos, France 168 Les Deux Alpes, France 378 Winter Park, CO, USA 499 Whistler, BC, Canada ... 

country column:

0 France 168 France 378 France 499 Australia 602 France ... 

My Code:

winners_df = df.loc[df['finish_position'] == 1, ['country', 'round_loc']] hometown_win = winners_df['country'].isin(winners_df['round_loc']) # Also tried hometown_win = winners_df['country'].isin(winners_df['round_loc'].values) print(hometown_win) 

My results:

0 False 168 False 378 False 499 False 602 False ... 

Not sure what I'm doing wrong.

winners_df['country'][0] in winners_df['round_loc'][0] 

Works fine. I'm sure I could just do it with a loop but I feel like I'm missing something here.

1
  • 3
    This won't work isin is looking for exact matches, you want to compare each row and use contains to test for membership/matches here Commented Apr 6, 2017 at 7:59

1 Answer 1

1
print (winners_df) round_loc country 0 Val d'Allos, France France 168 Les Deux Alpes, France USA <-changed data sample 378 Winter Park, CO, USA France 499 Whistler, BC, Canada Australia 

If need check if in column round_loc is one value from column country:

a = '|'.join(winners_df['country'].unique().tolist()) print (a) France|USA|Australia hometown_win = winners_df['round_loc'].str.contains(a) print(hometown_win) 0 True 168 True 378 True 499 False Name: round_loc, dtype: bool 

If need check if in column round_loc is one value from column country, but per row:

hometown_win = winners_df.apply(lambda x: x['country'] in x['round_loc'],axis=1) print(hometown_win) 0 True 168 False 378 False 499 False dtype: bool 
Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh that makes sense. Theres seems like there might be a problem here if there is a race in France (round_loc) that isn't won by someone in France (country) as each row contains a racer.
That's perfect! Thank you! I didn't know you could check all the items on an axis like that!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.