We select multiple columns to look at or work with specific parts of a table of data. This helps us focus on the information we need.
0
0
Selecting multiple columns in Pandas
Introduction
When you want to see only a few columns from a big table.
When you need to analyze or change data in certain columns.
When you want to create a smaller table with just the columns you need.
When preparing data for charts or reports that use specific columns.
Syntax
Pandas
df[["column1", "column2", "column3"]]
Use double square brackets [[ ]] to select multiple columns.
The columns inside must be in a list (inside [ ]).
Examples
Selects the columns named 'Name' and 'Age' from the DataFrame.
Pandas
df[["Name", "Age"]]
Selects three columns: 'City', 'Salary', and 'Department'.
Pandas
df[["City", "Salary", "Department"]]
Sample Program
This code creates a table with four columns. Then it selects only the 'Name' and 'City' columns and prints them.
Pandas
import pandas as pd # Create a simple DataFrame data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"], "Salary": [70000, 80000, 90000] } df = pd.DataFrame(data) # Select multiple columns selected_columns = df[["Name", "City"]] print(selected_columns)
OutputSuccess
Important Notes
Remember to use double brackets [[ ]] when selecting multiple columns, not single brackets.
If you use single brackets with a list, it will cause an error.
The order of columns in the list decides the order in the result.
Summary
Select multiple columns by passing a list of column names inside double brackets.
This helps you focus on the data you want to see or work with.
Always check your column names to avoid errors.