Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

Here is how I encountered the error:

df.loc[a_list][df.a_col.isnull()]

The type of a_list is Int64Index, it contains a list of row indexes. All of these row indexes belong to df.

The df.a_col.isnull() part is a condition I need for filtering.

If I execute the following commands individually, I do not get any warnings:

df.loc[a_list]

df[df.a_col.isnull()]

But if I put them together df.loc[a_list][df.a_col.isnull()], I get the warning message (but I can see the result):

Boolean Series key will be reindexed to match DataFrame index

What is the meaning of this error message? Does it affect the result that is returned?

2 Answers

0 votes
by (107k points)

Your approach will work but notwithstanding the warning, but it's best not to rely on implicit, unclear action.

Solution for this is to make the selection of indices in a_list a boolean mask:

df[df.index.isin(a_list) & df.a_col.isnull()]

0 votes
by (3.1k points)

First of all lets Understand the Warning 
Boolean Indexing: When you apply any condition to a DataFrame it returns a Boolean Series For example: 
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6] #code to create a dataframe 
condition = df['A'] > 1 # This will create a boolean series 
Index Mismatch: If the Boolean Series is generated from a DataFrame or Series with an index other than the one you are filtering, pandas will issue a warning and reindex the Boolean Series by index to the DataFrame. 

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...