2

I have a dataset and I want to delete rows from the dataframe based on 2 or column values of that row . For example - I have data frame about all the TV Shows in US , and I need to delete the a particular rows of a TV Show based on the the season of the TV SHow and the episode . Like I need to delete rows of the TV SHow - Gotham but only the rows containing season 4 and episode 10 .

Would really appreciate if I could get help regarding this .

jinsi
  • 123
  • 9
  • This is not really a duplicate of that question. That question asked about conditioning on a single column value. This question asks about a condition that involves multiple columns. – Bob Baxley Jan 29 '16 at 00:51

1 Answers1

0

You just need a boolean series to index the dataframe. Something like:

conditions = np.logical_and(
     df.col1 == "Gotham",
     df.col2 == 4)

# or, if you need more than two columns in your condition do this:
conditions = np.all(np.array(
    [df.col1 == "Gotham",
     df.col2 == 4,
     df.col3 == 10]), axis=0)

df = df[~conditions]

Addressing the follow up question below, we have

conditions = np.logical_and(
     df.name == "Gotham",
     df.year == 2011)

df["model_ind"][conditions] = 1
Bob Baxley
  • 3,551
  • 1
  • 22
  • 28
  • Thank you Bob for the reply , really appreciate that , the code worked fine , – jinsi Jan 28 '16 at 18:28
  • No problem! Feel free to accept the answer. – Bob Baxley Jan 28 '16 at 18:37
  • I was facing another issue , In this issue , I want to replace a value of a column named - 'model_ind' to 1 , If, the Name of the show is Gotham and the year is 2011. Would really appreciate if you could help me with it , Thank you, – jinsi Jan 28 '16 at 22:24
  • That was perfect , Thank you so much I would like to ask one last doubt to you , I have a column , called Lnc which is of type integer , and I want to create another column value in the same data frame named Log_Lnc which is log base2 value of the column Lnc . Would really appreciate if you could help me with that – jinsi Jan 29 '16 at 02:28
  • And i was trying to apply the same logic you explained to the following code, But it was not working - if mdy(9,24,2007)<=(target_date) and (target_date) – jinsi Jan 29 '16 at 02:29
  • Can you edit your question with a description of the problem? – Bob Baxley Jan 29 '16 at 02:31
  • I had 2 different issues : 1)I I was trying to apply the logic you explained in the above code about replacing the value of column , to the following code, if mdy(9,24,2007)<=(target_date) and (target_date) – jinsi Jan 29 '16 at 02:59