Drop values from a dataframe based on a boolean value

Asked

Viewed 37 times

1

When I read a CSV file, these are the first 5 lines:

     Shape Reported             Time
1          OTHER  6/30/1930 20:00
3           DISK   6/1/1931 13:00
4          LIGHT  4/18/1933 19:00
5           DISK  9/15/1934 15:30
6         CIRCLE   6/15/1935 0:00

I want to drop all values that are equal to 'OTHER' in the column Reported Shape, but when I try this:

ufo.drop(ufo['Shape Reported'] == 'OTHER', axis=0, inplace=True)

I get this mistake:

  File "C:\CursoPython\venv\lib\site-packages\pandas\core\indexes\base.py", line 5591, in drop
    raise KeyError(f"{labels[mask]} not found in axis")
KeyError: '[False False False ... False False False] not found in axis'

Is there any way to use the drop to do this? If so, it is possible not to use ILOC, trying to do something similar to what I did?

1 answer

1

It is possible to perform this task without using drop, selected the lines of interest.

ufo = ufo[ufo["Shape Reported "] != "OTHER"]

Thus the new Ufo will have all lines, except those that contain "OTHER" in the column "Shape Reported"

This type of selection is called Boolean Indexing, here in the documentation of Pandas has the detailed explanation, this in English but you can use the translation of the browser itself if you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.