Skip to main content

How to Select Several Columns of a pandas DataFrame with a List

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

To work with only some of a table's columns, give pandas a list of their names inside the square brackets: df[["name", "city"]]. You get a smaller table back, with the columns in the order of your list. This guide does it with pandas 3.1.2 on Python 3.15.2.

Syntax​

Syntax
<smaller table> = <table>[["<column 1>", "<column 2>"]]
PartWhat it means
The outer [ ]Selects from the table
The inner [ ]A Python list of column names
The order of the namesThe order of the columns in the result

The indexing guide says: "You can pass a list of columns to [] to select columns in that order."

The Example​

A table of two people has a name, a city and an age. The script keeps only the name and the city.

Prerequisites​

  • pandas in your project, for example with uv add pandas. This guide uses pandas 3.1.2 on Python 3.15.2.

Step 1: Write the Script​

several_columns.py
import pandas as pd

people = pd.DataFrame(
{"name": ["Ada", "Grace"], "city": ["London", "New York"], "age": [36, 45]}
)

print(people[["name", "city"]])

The several_columns.py script in a macOS-style code window: a people DataFrame with name, city and age, and print(people[["name", "city"]]).

Step 2: Run It​

Terminal
uv run python several_columns.py
Output
name city
0 Ada London
1 Grace New York

The terminal after uv run python several_columns.py: a table with the name and city columns only, for Ada in London and Grace in New York.

The age column is gone, and the original table is unchanged. The command works unchanged in PowerShell.

Choose the Order​

The list sets the order. people[["city", "name"]] printed:

Output
city name
0 London Ada
1 New York Grace

Common Mistakes​

MistakeWhat happensDo this instead
Single brackets: people["name", "city"]KeyError: ('name', 'city'): pandas looks for one column whose name is that pairUse double brackets: people[["name", "city"]]
A name that isn't a column: people[["name", "town"]]KeyError: "['town'] not in index"Check the names in people.columns

Troubleshooting​

SymptomLikely causeFix
KeyError: ('name', 'city')The names are in single bracketsAdd the inner list brackets
KeyError: "['...'] not in index"One of the listed names doesn't existFix the spelling; the error names the missing column

FAQ​

Does it change the original table?​

No. It returns a new, smaller table. After smaller = people[["name", "city"]], list(people.columns) still printed ['name', 'city', 'age'].

Can I keep the list in a variable?​

Yes, and that's common when the same columns are used in several places: columns = ["name", "city"], then people[columns].

Conclusion​

people[["name", "city"]] returned a table with only those two columns, in the order of the list. Use double brackets, and read the KeyError to find a misspelled name.

Additional Resources​