close

[Solved] Pandas: IndexingError: Unalignable boolean Series provided as indexer

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Pandas: IndexingError: Unalignable boolean Series provided as indexer in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Pandas: IndexingError: Unalignable boolean Series provided as indexer Error Occurs?

Today I get the following error Pandas: IndexingError: Unalignable boolean Series provided as indexer in python.

How To Solve Pandas: IndexingError: Unalignable boolean Series provided as indexer Error ?

  1. How To Solve Pandas: IndexingError: Unalignable boolean Series provided as indexer Error ?

    To Solve Pandas: IndexingError: Unalignable boolean Series provided as indexer Error This will drop any column which doesn't have at least 1 non-NaN value which will mean any column with all NaN will get dropped

  2. Pandas: IndexingError: Unalignable boolean Series provided as indexer

    To Solve Pandas: IndexingError: Unalignable boolean Series provided as indexer Error This will drop any column which doesn't have at least 1 non-NaN value which will mean any column with all NaN will get dropped

Solution 1


You need loc, because filter by columns:

print (df.notnull().any(axis = 0))
a     True
b     True
c     True
d    False
dtype: bool

df = df.loc[:, df.notnull().any(axis = 0)]
print (df)

     a    b    c
0  1.0  4.0  NaN
1  2.0  NaN  8.0
2  NaN  6.0  9.0
3  NaN  NaN  NaN

Or filter columns and then select by []:

print (df.columns[df.notnull().any(axis = 0)])
Index(['a', 'b', 'c'], dtype='object')

df = df[df.columns[df.notnull().any(axis = 0)]]
print (df)

     a    b    c
0  1.0  4.0  NaN
1  2.0  NaN  8.0
2  NaN  6.0  9.0
3  NaN  NaN  NaN

Or dropna with parameter how='all' for remove all columns filled by NaNs only:

print (df.dropna(axis=1, how='all'))
     a    b    c
0  1.0  4.0  NaN
1  2.0  NaN  8.0
2  NaN  6.0  9.0
3  NaN  NaN  NaN

Solution 2

u can use dropna with axis=1 and thresh=1:

In[19]:
df.dropna(axis=1, thresh=1)

Out[19]: 
     a    b    c
0  1.0  4.0  NaN
1  2.0  NaN  8.0
2  NaN  6.0  9.0
3  NaN  NaN  NaN

This will drop any column which doesn’t have at least 1 non-NaN value which will mean any column with all NaN will get dropped

The reason what you tried failed is because the boolean mask:

In[20]:
df.notnull().any(axis = 0)

Out[20]: 
a     True
b     True
c     True
d    False
dtype: bool

cannot be aligned on the index which is what is used by default, as this produces a boolean mask on the columns

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read