Pandas Filter Rows

I am using pandas 0.13.1 Python 2.7: In df there are values ​​in the df.risk column, which are also not: Small, Medium or High. I want to delete rows with a value other than Small, Medium, and High. I tried

df = df[(df.risk == "Small") | (df.risk == "Medium") | (df.risk == "High")]

But this returns an empty df. How can I filter correctly?

+4
source share
1 answer

I think you want:

df = df[(df.risk.isin(["Small","Medium","High"]))]

Example:

In [5]:
import pandas as pd
df = pd.DataFrame({'risk':['Small','High','Medium','Negligible', 'Very High']})
df

Out[5]:

         risk
0       Small
1        High
2      Medium
3  Negligible
4   Very High

[5 rows x 1 columns]

In [6]:

df[df.risk.isin(['Small','Medium','High'])]

Out[6]:

     risk
0   Small
1    High
2  Medium

[3 rows x 1 columns]
+3
source

Source: https://habr.com/ru/post/1538269/


All Articles