Reputation: 1356
I have a data frame that contains 5 Player columns which contain random player names. I want to be able to pass in a list of players and only return rows where both players are present in that row (across those 5 columns).
This is the code that generates the dataframe and successfully filters out rows that contain either person in the row. How would I go about making sure that the row contains both people?
random_events = ('SHOT', 'MISSED_SHOT', 'GOAL')
random_team = ('Preferred', 'Other')
events = list()
for i in range(6):
event = dict()
event['event_type'] = random.choice(random_events)
event['team'] = random.choice(random_team)
event['coords_x'] = round(random.uniform(-100, 100), 2)
event['coords_y'] = round(random.uniform(-42.5, 42.5), 2)
event['person_1'] = f'Person {random.randint(1, 2)}'
event['person_2'] = f'Person {random.randint(3, 4)}'
event['person_3'] = f'Person {random.randint(5, 6)}'
event['person_4'] = f'Person {random.randint(7, 8)}'
event['person_5'] = f'Person {random.randint(9, 10)}'
events.append(event)
df = pd.DataFrame(events)
print(df)
filter_list = ['Person 1', 'Person 3']
filtered_df = df.loc[
(df['person_1'].isin(filter_list)) |
(df['person_2'].isin(filter_list)) |
(df['person_3'].isin(filter_list)) |
(df['person_4'].isin(filter_list)) |
(df['person_5'].isin(filter_list))]
print(filtered_df)
This is the result I get -- showing rows with only Person 1 or Person 3, as well as Person 1 and Person 3 are returned. In the example below I would only want Row with Index 2 returned to me
coords_x coords_y event_type person_1 person_2 person_3 person_4 person_5 team
0 38.82 -39.18 MISSED_SHOT Person 1 Person 4 Person 6 Person 7 Person 9 Preferred
2 94.43 30.13 GOAL Person 1 Person 3 Person 5 Person 8 Person 9 Other
3 -68.38 -24.42 MISSED_SHOT Person 2 Person 3 Person 5 Person 7 Person 10 Preferred
4 99.48 22.79 SHOT Person 1 Person 4 Person 5 Person 7 Person 9 Preferred
Thank you in advance.
Upvotes: 1
Views: 627
Reputation: 145
Here's a general approach. You'll probably want to play around with it depending on your exact situation.
# Define a list of all of the person columns in the dataframe
person_cols = [f'person_{i}' for i in [1, 2, 3, 4, 5]]
# Which rows contain Person 2 in any column? (creates a series of True or False)
(df[person_cols] == "Person 2").any(axis='columns')
# Which rows contain both Person 2 and Person 3?
# This time I'm saving the series to use as a selection mask
mask = (
(df[person_cols] == "Person 2").any(axis='columns')
& (df[person_cols] == "Person 3").any(axis='columns')
)
# show just the rows where the mask above is True
print(df[mask])
Setting up a mask for an arbitrary list of players who must all be present.
from operator import and_
from functools import reduce
players = ['Player 1', 'Player 3', 'Player 4']
filters = [(df[person_cols] == p).any(axis='columns') for p in players]
mask = reduce(and_, filters, True)
print(df[mask])
Upvotes: 2