Python

Pandas error "Unalignable boolean Series provided as indexer"

Share this for your friends.

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

Author


Mid-carieer engineer (AI, system). Good at Python and SQL.

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


1 row is extracted.


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


Hmm? Now it shows error.


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


Now this error is solved.


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.


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



There are some other articles about pandas.Dataframe.

If you interested in them, please read them.


Share this for your friends.

If you felt this article is useful, please share.

にほんブログ村 IT技術ブログへ

-Python
-,

© 2023 ITips