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 .

1
  • 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. Commented Jan 29, 2016 at 0:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Bob for the reply , really appreciate that , the code worked fine ,
No problem! Feel free to accept the answer.
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,
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
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)<mdy(9,22,2008) then Season=2007;
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.