1

I have a data frame (other_team_df) of premier league teams and I want to drop rows where the home team is either: Arsenal, Chelsea, Liverpool, Tottenham, Man City or Man United. When I run the code below, del_row_index has length=1596 and other_team_df has 5321 rows. So im expecting 3725 rows to be left after the drop. However, I only get back 72 rows and I'm not sure why enter image description here

del_row_index=other_team_df[(other_team_df['HomeTeam']=='Arsenal') |(other_team_df['HomeTeam']=='Chelsea')|
              (other_team_df['HomeTeam']=='Liverpool') |(other_team_df['HomeTeam']=='Tottenham')| 
              (other_team_df['HomeTeam']=='Man City')| (other_team_df['HomeTeam']=='Man United')].index

other_team_df.drop(del_row_index)

1 Answers1

0

Use isin and boolean indexing:

to_drop = ['Arsenal', 'Chelsea', 'Liverpool', 'Tottenham', 'Man City', 'Man United']

out = other_team_df.loc[~other_team_df['HomeTeam'].isin(to_drop)]
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Than you for your help. I wanted the teams that were not in the list so i put "~" infront of "other_team_df['HomeTeam']" for it to locate all teams that were not in the above list which gave me what I was looking for. – Immanuel Nii Odai Odarteifio Nov 27 '22 at 18:32
  • 1
    @Immanuel yes of course, that's a typo from my side! `~` is needed – mozway Nov 27 '22 at 19:17