Pandas error "Unalignable boolean Series provided as indexer"

Why does pandas say "Unalignable boolean Series provided" error ?

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 raised such an error.

IndexingError: Unalignable boolean Series provided as indexer 
(index of the boolean Series and of the indexed object do not match).

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'".

目次

"Unalignable boolean Series provided as indexer" error

"Unalignable boolean Series provided as indexer" error

First, what is "Unalignable boolean Series provided as indexer" error ?

It happens when we filter pandas DataFrame.

In order to explain it, we should prepare sample DataFrame.

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[].

df1_part = df1.loc[
  df1["col"] ==  "a"
]
print(df1_part)

#   col
# 0   a

1 row is extracted.

Then prepare another DataFrame.

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

And try to extract from this DataFrame.

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).

Hmm? Now it shows error.

This is "Unalignable boolean Series provided as indexer" error.

So what is the cause of the error ?

The reason of Pandas error "Unalignable boolean Series provided as indexer"

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.

The cause of the error is the partdf2.loc[ df1["col"] == "e" ] .
We tried to compare 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.

print(df2)

#   col
# 0   e
# 1   f
# 2   g
# 3   h
# 4   i

print(df1["col"] ==  "e")

# 0    False
# 1    False
# 2    False
# 3    False
# Name: col, dtype: bool

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).

Now how do you solve this error ?

How to solve "Unalignable boolean Series provided as indexer" error

How to solve "Unalignable boolean Series provided as indexer" error

In order to solve "Unalignable boolean Series provided as indexer" error, we should set same count elements as parameter of loc[].

print(df2)

#   col
# 0   e
# 1   f
# 2   g
# 3   h
# 4   i

print(df2["col"] ==  "e")

# 0     True
# 1    False
# 2    False
# 3    False
# 4    False
# Name: col, dtype: bool

df2_part = df2.loc[
  df2["col"] ==  "e"
]
print(df2_part)

#   col
# 0   e

Now df2["col"] == "e" returns 5 elements boolean Series.
So the filtered DataFrame df2.loc[df2["col"] == "e"] doesn't occur error.

Now this error is solved.

Conclusion

Conclusion

Today I explained about "Pandas error 'Unalignable boolean Series provided as indexer'".

The reason of the error is following.

If condition boolean Series count does not match with DataFrame, it causes error.

Its error message Unalignable boolean Series provided as indexer is difficult to understand…

Python Indexing Question : learnpython

この記事が気に入ったら
いいね または フォローしてね!

If you like this article, please share !
  • URLをコピーしました!
  • URLをコピーしました!

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

目次