Cannot subset columns with tuple with more than one element

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Cannot subset columns with tuple with more than one element

The article addresses the following 2 equivalent errors:

  • ValueError: Cannot subset columns with a tuple with more than one element. Use a list instead.
  • Future Warning: Indexing with multiple keys

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.

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

cannot subset columns with tuple with more than one element

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.

main.py
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) )
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
Animal Max Speed Animal Cat 0 Cat 25 1 Cat 35 2 Cat 45 Dog 3 Dog 55 4 Dog 65 5 Dog 75

use two sets of square brackets when selecting multiple columns

If you need to groupby using multiple columns, pass a list of column names to the DataFrame.groupby() method.

main.py
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) )
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
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

groupby with multiple columns correctly

Always make sure to use two sets of square brackets when selecting multiple columns after calling DataFrame.groupby().

For example, the following is correct.

main.py
# ✅ 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.

main.py
# ⛔️ 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.

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