Last updated: Apr 12, 2024
Reading time·3 min
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.
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])
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
.
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']])
You can also use a one-dimensional boolean indexer when using DataFrame.loc
.
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']])
You can also access specific rows directly, using two sets of square brackets.
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]])
The code sample selects the rows at index 0
and 2
.
This also works with Series
.
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']])
You might also get the error when you try to incorrectly rename the columns in a
DataFrame
.
Here is an example.
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.
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)
You can learn more about the related topics by checking out the following tutorials: