Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

df.loc filtering doesn't work with None values

Why does this filtering not work when the filter is Project ID == None? I also noticed is None rather than == None returns KeyError: False

import pandas as pd
df = pd.DataFrame(data = [['Project1', 'CT', 800], [None, 3, 1000], ['Project3', 'CA', 20]], columns=['Project ID', 'State', 'Cost'])

print df.loc[df['Project ID'] == 'Project1'].values
print df.loc[df['Project ID'] == None].values

output:

[['Project1' 'CT' 800L]]
[]
like image 513
user2242044 Avatar asked Jul 30 '15 19:07

user2242044


People also ask

How do you filter DataFrame based on NaN values?

Filter out NAN Rows Using DataFrame. Filter out NAN rows (Data selection) by using DataFrame. dropna() method. The dropna() function is also possible to drop rows with NaN values df. dropna(thresh=2) it will drop all rows where there are at least two non- NaN .

How do you filter blanks in pandas?

To filter out the rows of pandas dataframe that has missing values in Last_Namecolumn, we will first find the index of the column with non null values with pandas notnull() function. It will return a boolean series, where True for not null and False for null values or missing values.

How do you filter a LOC in Python?

to filter one column by multiple values. df. loc[df['channel']. apply(lambda x: x in ['sale','fullprice'])] would also work.

Is DF LOC inclusive?

loc is end-inclusive, we can just say .

How do I filter the data in Loc [].?

We can specify a single label or a list of labels to filter the data in loc []. The filtered data can be of different types: a single value, a Series, or a DataFrame, as shown in the examples, respectively. When only a label or a list of labels is set, it will return all columns. Another common method is using the ranges of row and column labels.

How to filter the None values present in the ‘job profile’ column?

We have filtered the None values present in the ‘Job Profile’ column using filter () function in which we have passed the condition df [“Job Profile”].isNotNull () to filter the None values of the Job Profile column.

What is not in filter in pandas?

In this article, we will discuss NOT IN filter in pandas, NOT IN is a membership operator used to check whether the data is present in dataframe or not. It will return true if the value is not present, otherwise false

What is LOC property in Dataframe?

property DataFrame. loc Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used with a boolean array.


1 Answers

You have to use isnull for this:

In [3]:

df[df['Project ID'].isnull()]
Out[3]:
  Project ID State  Cost
1       None     3  1000

Or use apply:

In [5]:

df.loc[df['Project ID'].apply(lambda x: x is None)]
Out[5]:
  Project ID State  Cost
1       None     3  1000
like image 192
EdChum Avatar answered Sep 30 '22 16:09

EdChum