过滤列表中的多个数据框列

时间:2018-11-03 21:57:54

标签: python pandas dataframe

我有一个数据框,其中包含5个 Player 列,其中包含随机播放器名称。我希望能够传递球员列表,并且只返回该行中同时存在两个球员的行(横跨这5列)。

这是生成数据框并成功过滤出包含该行中任何人的行的代码。我将如何确保该行包含两个人?

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)

这是我得到的结果-显示仅包含Person 1或Person 3的行,以及返回的Person 1和Person 3。在下面的示例中,我只希望将索引为2的行返回给我

   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

谢谢。

1 个答案:

答案 0 :(得分:2)

这是一种通用方法。您可能要根据自己的实际情况来尝试使用它。

# 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])

编辑:

为所有必须出席的任意玩家列表设置遮罩。

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])