Pandas ValueError: Cannot index with multidimensional key

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Pandas ValueError: Cannot index with multidimensional key

The Pandas "ValueError: Cannot index with multidimensional key" occurs when you use the DataFrame.loc indexer with a multidimensional key.

To solve the error, access the specific DataFrame column when using DataFrame.loc.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) df2 = pd.DataFrame({ 'a': [0, 2] }) # ⛔️ ValueError: Cannot index with multidimensional key print(df.loc[df2])

value error cannot index with multidimensional key

We tried to pass a DataFrame to the DataFrame.loc indexer which caused the error.

Instead, access the specific column of the DataFrame when using df.loc.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) df2 = pd.DataFrame({ 'a': [0, 2] }) # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 print(df.loc[df2['a']])

access specific dataframe column when using df loc

The code for this article is available on GitHub

You can also use a one-dimensional boolean indexer when using DataFrame.loc.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) df2 = pd.DataFrame({ 'a': [True, False, False] }) # first_name salary experience # 0 Alice 175.1 10 print(df.loc[df2['a']])

using one dimensional boolean array indexer with df loc

You can also access specific rows directly, using two sets of square brackets.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 print(df.loc[[0, 2]])

access specific rows directly using two sets of square brackets

The code for this article is available on GitHub

The code sample selects the rows at index 0 and 2.

This also works with Series.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) df2 = pd.DataFrame({ 'a': pd.Series([0, 2]) }) # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 print(df.loc[df2['a']])

also works with series

# Incorrectly renaming the columns in a DataFrame

You might also get the error when you try to incorrectly rename the columns in a DataFrame.

Here is an example.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) # ⛔️ Incorrect df.columns = [['name', 'salary', 'experience']]

Notice that we have 2 sets of square brackets on the right-hand side.

Instead, specify the column names in 1 set of square brackets.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'experience': [10, 15, 20] }) # ✅ only using one set of square brackets df.columns = ['name', 'salary', 'experience'] # name salary experience # 0 Alice 175.1 10 # 1 Bobby 180.2 15 # 2 Carl 190.3 20 print(df)

specify column names in one set of square brackets

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.