If I understand you correctly, you want to filter your dataframe to return only the rows that a player's name appears, in any of the columns. If that, you can utilise apply()
, which is not efficient, and any()
, which will search for any condition being True
, as demonstrated below:
player_name = 'DYLAN.LARKIN'
new = df[df[col_list].apply(lambda i: i.str.contains(player_name).any(), axis=1)]
axis=1
refers to row-wise computations.
Printing back new
:
home_on_1 home_on_2 home_on_3 ... away_on_5 away_on_6 away_on_7
0 DYLAN.LARKIN John John ... John DYLAN.LARKIN John
1 John John John ... John John John
2 John John John ... John John John
3 John John John ... John John John
4 John DYLAN.LARKIN John ... John John DYLAN.LARKIN
5 John John John ... John John John
6 John John John ... John John John
[7 rows x 14 columns]
will return back only the rows that contain at least once the player name you declare.
Sample DF
:
{'home_on_1': {0: 'DYLAN.LARKIN',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'home_on_2': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'DYLAN.LARKIN',
5: 'John',
6: 'John'},
'home_on_3': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'home_on_4': {0: 'John',
1: 'DYLAN.LARKIN',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'home_on_5': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'DYLAN.LARKIN'},
'home_on_6': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'home_on_7': {0: 'DYLAN.LARKIN',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'away_on_1': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'DYLAN.LARKIN',
5: 'John',
6: 'John'},
'away_on_2': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'away_on_3': {0: 'John',
1: 'DYLAN.LARKIN',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'away_on_4': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'DYLAN.LARKIN'},
'away_on_5': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'away_on_6': {0: 'DYLAN.LARKIN',
1: 'John',
2: 'John',
3: 'John',
4: 'John',
5: 'John',
6: 'John'},
'away_on_7': {0: 'John',
1: 'John',
2: 'John',
3: 'John',
4: 'DYLAN.LARKIN',
5: 'John',
6: 'John'}}