Last updated: Apr 12, 2024
Reading time·3 min
The article addresses the following 2 equivalent errors:
If you use a Pandas version less than 2.0.0
, you will get the "Future Warning:
Indexing with multiple keys".
If your Pandas version is greater than 2.0.0
, you will get a ValueError
exception.
The "ValueError: Cannot subset columns with a tuple with more than one element. Use a list instead." occurs when you use single, instead of double square brackets when selecting multiple columns.
To solve the error, make sure to use two sets of square brackets when selecting multiple columns.
Here is an example of how the error occurs.
import pandas as pd df = pd.DataFrame({ 'ID': [1, 1, 1, 2, 2, 2], 'Animal': ['Cat', 'Cat', 'Cat', 'Dog', 'Dog', 'Dog'], 'Max Speed': [25, 35, 45, 55, 65, 75] }) # ⛔️ Future Warning: Indexing with multiple keys # ⛔️ ValueError: Cannot subset columns with a tuple with more than one element. Use a list instead. print(df.groupby('Animal')['Animal', 'Max Speed'].apply(lambda x: x))
Notice that we used a single set of square brackets when selecting the Animal
and Max Speed
columns.
Instead, use two sets of square brackets to solve the error.
import pandas as pd df = pd.DataFrame({ 'ID': [1, 1, 1, 2, 2, 2], 'Animal': ['Cat', 'Cat', 'Cat', 'Dog', 'Dog', 'Dog'], 'Max Speed': [25, 35, 45, 55, 65, 75] }) print( df.groupby('Animal')[['Animal', 'Max Speed']].apply(lambda x: x) )
Running the code sample produces the following output.
Animal Max Speed Animal Cat 0 Cat 25 1 Cat 35 2 Cat 45 Dog 3 Dog 55 4 Dog 65 5 Dog 75
If you need to
groupby
using multiple columns, pass a list of column names to the DataFrame.groupby()
method.
import pandas as pd df = pd.DataFrame({ 'ID': [1, 1, 1, 2, 2, 2], 'Animal': ['Cat', 'Cat', 'Cat', 'Dog', 'Dog', 'Dog'], 'Max Speed': [25, 35, 45, 55, 65, 75] }) print( df.groupby( ['ID', 'Animal'] )[['Animal', 'Max Speed']].apply(lambda x: x) )
Running the code sample produces the following output.
Animal Max Speed ID Animal 1 Cat 0 Cat 25 1 Cat 35 2 Cat 45 2 Dog 3 Dog 55 4 Dog 65 5 Dog 75
Always make sure to use two sets of square brackets when selecting multiple
columns after calling DataFrame.groupby()
.
For example, the following is correct.
# ✅ Correct used 2 sets of square brackets [[]] print( df.groupby('Animal')[['Animal', 'Max Speed']].apply(lambda x: x) )
And the following is incorrect and causes the error.
# ⛔️ Incorrect only used 1 set of square brackets [] after groupby print( df.groupby('Animal')['Animal', 'Max Speed'].apply(lambda x: x) )
Two sets of square brackets [[]]
are used to output a DataFrame
, whereas one
set of square brackets []
is used to output a Series
.
You can learn more about the related topics by checking out the following tutorials: