Solve the error of panda index unalignable Boolean series provided as indexer

Complete error reports are as follows: pandas core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Solution:

There are two indexing methods, one of which can be selected:

Solution 1

dataframe[pd.Series([True, True, True], index=dataframe.index)]

Solution 2:

dataframe.loc[pd.Series([True, True], index=['a', 'b']).index]

Problem analysis

An error will appear in the following code:

import pandas as pd
import numpy as np


dataframe = pd.DataFrame(data=np.random.random(size=(3, 5)), index=['a', 'b', 'c'])

Error codes are as follows:

dataframe[pd.Series([True, True], index=['a', 'b'])]

You can see that the original dataframe includes 3 lines, but there are only two true values here, so an error is reported. The correct method is:

dataframe[pd.Series([True, True, True], index=['a', 'b', 'c'])]
dataframe.loc[pd.Series([True, True], index=['a', 'b']).index]

Read More: