I saw good posts that perfectly answer my title question (including this one ), but I am in a more specific situation.
Let's say I have the following very simple DataFrame
df.head()
param accuracy
0 None 98
1 4.0 100
2 5.0 95
3 6.0 87
4 7.0 56
5 8.0 45
6 9.0 59
7 None 96
...
I would like to restrict my DataFrame to data where param is either None or 4. I tried the following technique
params = [None, 4]
df = df[df['param'].isin(params)]
which only selects data where param is 4.
This post shows how to filter None values with isnull()
method, but it is not compatible with isin()
... Hence my question.
You can use "and" and "or" operations on the selectors and construct new ones. Would this help in your case?
params = [4]
df = df[df['param'].isin(params) | df['param'].isnull()]
As pointed out by @IMB, a solution is to do params = ["None", 4]
instead of params = [None, 4]
.
My dataframe was initially containing NaN, which I transformed into None with df = df.fillna('None')
. Hence the String type.
Try this:
df = df[ (df['param'] == 4) | (df['param'].isna()) ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.