0

I am just a beginner so I apologize for this remedial question. I am wanting to drop some observations from a dataframe.

Is there a way to combine these two lines in to one in python?

NHL_Team_R_Stats=NHL_Team_R_Stats[NHL_Team_R_Stats.competition_name != '2011 NHL Regular Season']
NHL_Team_R_Stats=NHL_Team_R_Stats[NHL_Team_R_Stats.competition_name != '2012 NHL Regular Season']

I tried

NHL_Team_R_Stats=NHL_Team_R_Stats[NHL_Team_R_Stats.competition_name != ('2011 NHL Regular Season','2012 NHL Regular Season')]

but it wouldn't work.

Thanks for your help!

Jeff Jones
  • 11
  • 3

2 Answers2

1
NHL_Team_R_Stats=NHL_Team_R_Stats[(NHL_Team_R_Stats.competition_name != '2010 NHL Regular Season') & (NHL_Team_R_Stats.competition_name != '2011 NHL Regular Season')]

the above worked

Jeff Jones
  • 11
  • 3
1

Using the keyword "isin" and the negation "~" should do the job.

# the values you want to filter out
val = ["2011 NHL Regular Season", "2012 NHL Regular Season"]

# ~ returns the inverse of the mask
NHL_Team_R_Stats=NHL_Team_R_Stats.loc[~NHL_Team_R_Stats.competition_name.isin(val)]
klegoff
  • 51
  • 3