
pandas.DataFrame
is useful to handle table format data.
With using pandas, we can import CSV or Excel data and extract specific rows.
One day, pandas said such an error.
This error message was difficult for us to understand.
What was the cause of error ?
So today I will introduce about "Pandas error 'Unalignable boolean Series provided as indexer'".
Author

Advantage to read
You can understand about Pandas error "Unalignable boolean Series provided as indexer". Then you don't have to concern about programming with pandas.DataFrame.
The reason of Pandas error "Unalignable boolean Series provided as indexer"
The reason of Pandas error "Unalignable boolean Series provided as indexer".
It is because the count of boolean Series does not match with the count of DataFrame.
In order to explain it, we should prepare sample DataFrame.
DATA
import pandas as pd data_list1 = [ ["a"], ["b"], ["c"], ["d"] ] col_list1 = ["col"] df1 = pd.DataFrame(data=data_list1, columns=col_list1) print(df1) # col # 0 a # 1 b # 2 c # 3 d
Next, extract specific rows by .loc[]
.
SAMPLE
df1_part = df1.loc[ df1["col"] == "a" ] print(df1_part) # col # 0 a

Then prepare another DataFrame.
DATA
data_list2 = [ ["e"], ["f"], ["g"], ["h"], ["i"], ] col_list2 = ["col"] df2 = pd.DataFrame(data=data_list2, columns=col_list2) print(df2) # col # 0 e # 1 f # 2 g # 3 h # 4 i
Then try to extract from this DataFrame.
SAMPLE
df2_part = df2.loc[ df1["col"] == "e" ] print(df2_part) # IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

The cause of the error is df1["col"] == "e"
.
We tried to compair df2
with "e"
, but we wrote df1
.
df2
has 5 rows.
And the condition df1["col"] == "e"
returns 4 elements boolean Series.
Elements count does not match.
So it raised error.
In order to modify it, we should set same count elements.
SAMPLE
df2_part = df2.loc[ df2["col"] == "e" ] print(df2_part) # col # 0 e

Conclusion
Today I will explained about "Pandas error 'Unalignable boolean Series provided as indexer'".
The reason of the error is following.
Note
- If condition boolean Series count does not match with DataFrame, it causes error.

Unalignable boolean Series provided as indexer
is difficult to understand...Reference
There are some other articles about pandas.Dataframe.
If you interested in them, please read them.
Read more