Reputation: 940
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.
Upvotes: 1
Views: 1644
Reputation: 862851
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
Upvotes: 1