How to Select Multiple Columns in pandas DataFrame
To select multiple columns in pandas, use
df[["col1", "col2"]] where df is your DataFrame and the list contains the column names you want. This returns a new DataFrame with only those columns.Syntax
Use df[["col1", "col2", ...]] to select multiple columns from a pandas DataFrame.
df: your DataFrame variable["col1", "col2", ...]: a list of column names you want to select- The result is a new DataFrame with only the selected columns
python
df[["column1", "column2"]]
Example
This example shows how to create a DataFrame and select two columns from it.
python
import pandas as pd data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) selected_columns = df[["Name", "City"]] print(selected_columns)
Output
Name City
0 Alice New York
1 Bob Los Angeles
2 Charlie Chicago
Common Pitfalls
One common mistake is using single brackets df["col1", "col2"] which causes an error because pandas expects a single column name or a list inside double brackets.
Another mistake is passing column names that do not exist in the DataFrame, which raises a KeyError.
python
import pandas as pd data = {"A": [1, 2], "B": [3, 4]} df = pd.DataFrame(data) # Wrong way - single brackets with multiple columns (raises error) # df[["A", "B"]] # Right way - double brackets with list selected = df[["A", "B"]] print(selected)
Output
A B
0 1 3
1 2 4
Quick Reference
Remember these tips when selecting multiple columns:
- Use double brackets
df[[...]]with a list of column names. - Column names must be exact and exist in the DataFrame.
- The result is always a DataFrame, not a Series.
Key Takeaways
Use double brackets with a list of column names to select multiple columns.
Passing non-existent column names causes a KeyError.
Single brackets with multiple columns is invalid syntax.
The output is a DataFrame containing only the selected columns.
Always check column names for typos before selecting.