0

I have this code:

Different_year = df[df['Start_year'] != (df['End_year'])]

And i just want to remove that condition from my main dataframe like this:

df.drop(['Different_year'])

Surprisingly it works but the dataframe still has the same shape

vando
  • 37
  • 6
  • 1
    df[df['Start_year'] == df['End_year']] ? – BENY Feb 13 '18 at 17:32
  • What do you mean by "I want to remove condition from dataframe"? Please, provide some minimal example data with desired output. – Georgy Feb 13 '18 at 17:36
  • Possible duplicate of [Pandas deleting row with df.drop doesn't work](https://stackoverflow.com/questions/38481409/pandas-deleting-row-with-df-drop-doesnt-work) – Georgy Feb 13 '18 at 17:36
  • 1
    @Georgy, seems like there's more than one thing wrong with what user is doing. So not exact duplicate unfortunately. – jpp Feb 13 '18 at 17:49
  • @jp_data_analysis Aha. You are right. I misunderstood the problem. – Georgy Feb 13 '18 at 17:56
  • So, it looks like it's a duplicate of this question then: https://stackoverflow.com/questions/13851535/how-to-delete-rows-from-a-pandas-dataframe-based-on-a-conditional-expression – Georgy Feb 13 '18 at 17:59
  • 1
    @Georgy, agreed, I'll vote to close, but will leave my answer for user to see. – jpp Feb 13 '18 at 18:01
  • 1
    Possible duplicate of [How to delete rows from a pandas DataFrame based on a conditional expression](https://stackoverflow.com/questions/13851535/how-to-delete-rows-from-a-pandas-dataframe-based-on-a-conditional-expression) – jpp Feb 13 '18 at 18:01

2 Answers2

0

The drop method does not work in place unless you specify so.

df.drop(different_year, inplace=True)

or assign it

df = df.drop(different_year)

Make sure your variable different_year are indices.

Diego Aguado
  • 1,604
  • 18
  • 36
0

Below solutions should work. Notice the following for Method 1:

  • We are feeding the index of selected rows into drop.
  • We assign back to df so the dataframe is actually modified.
  • axis=0 represents index-level drop. More information in df.drop documentation.

Method 1

different_year = df[df['Start_year'] != (df['End_year'])].index

df = df.drop(different_year, axis=0)

Method 2

df = df[~(df['Start_year'] != (df['End_year']))]
jpp
  • 159,742
  • 34
  • 281
  • 339