0

I'm working with a dataframe from a csv file and I want to drop a certain row. I tried using df2[df2.State != 'INDIA'], where INDIA is the row I want to drop. Here's an example of the dataframe:

                          State  Total population
10                      Gujarat          60383628
11                      Haryana          25353081
12             Himachal Pradesh           6856509
13                        INDIA        1210193422
14              Jammu & Kashmir          12548926
15                    Jharkhand          32966238
16                    Karnataka          61130704
17                       Kerala          33387677

What am I doing wrong?

Edit: There's no error but the change is not being reflected. It's still the exact same table. That worked @MohitMotwani. Is there any way to keep the index intact? 'INDIA'removed means that the index 13 is removed and hence the index is being displayed as 11, 12, 14, 15. Is this solvable?

Abhishek
  • 553
  • 2
  • 9
  • 26
  • 6
    You need to reassign it `df2=df2[df2.State != 'INDIA'] ` – Mohit Motwani Mar 24 '19 at 07:23
  • That worked @MohitMotwani. However, I still have a question. Is there any way to keep the index intact? INDIA removed means that the index 13 is removed and hence the index is being displayed as 11, 12, 14, 15. Is this solvable? – Abhishek Mar 24 '19 at 07:25
  • 4
    Use df2=df2. reset_index(drop=True) after the code – anky Mar 24 '19 at 07:28
  • 1
    Cheers @anky_91. Worked for me – Abhishek Mar 24 '19 at 07:30
  • 1
    @Abhishek: please edit that second part of your question into the question above. Don't modify your question intent in comments. – smci Mar 25 '19 at 10:06

2 Answers2

3

Another possible way to drop the row while keeping the axis intact in one line could be:

df2 = df2.query("State != 'INDIA'")
Loochie
  • 2,414
  • 13
  • 20
2

You should assign it to df2.

df2 = df2[df2.State != 'INDIA']

For more learning read How do I delete rows in a data frame? as well.

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57