Column Selection in Pandas
Learn how to select a single column, multiple columns, and specific groups of columns from a Pandas DataFrame.
1. What Is Column Selection?
Column selection means retrieving one or more columns from a DataFrame. It is useful when you want to work with only the information required for a particular task.
For example, Samadhan may need student names for an attendance report, while Pradnya may need names and marks to prepare a performance report. Pandas lets us select those columns without displaying the entire dataset.
Create a Sample DataFrame
import pandas as pd
df = pd.DataFrame({
"Name": ["Samadhan", "Pradnya", "Samiksha", "Amit"],
"Age": [21, 22, 20, 23],
"City": ["Pune", "Mumbai", "Pune", "Nashik"],
"Marks": [85, 92, 78, 88]
})
print(df)
Output
Name Age City Marks 0 Samadhan 21 Pune 85 1 Pradnya 22 Mumbai 92 2 Samiksha 20 Pune 78 3 Amit 23 Nashik 88
2. Select a Single Column
The simplest way to select one column is to use its name inside square brackets.
names = df["Name"] print(names)
Output
0 Samadhan 1 Pradnya 2 Samiksha 3 Amit Name: Name, dtype: object
The result is a Pandas Series, which is a
one-dimensional labeled data structure.
df["Name"] returns
a Series, not a DataFrame.
Select another column
print(df["Marks"])
Output
0 85 1 92 2 78 3 88 Name: Marks, dtype: int64
3. Select Multiple Columns
To select more than one column, provide a list of column names inside the outer square brackets.
selected = df[["Name", "Marks"]] print(selected)
Output
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78 3 Amit 88
The result is a DataFrame because it contains multiple columns.
df["Name", "Marks"] is not the normal way to
select two columns. Use
df[["Name", "Marks"]] instead.
4. Visual Guide: Single vs Multiple Columns
A single column selection returns a Series, whereas a list of column names returns a DataFrame containing those columns.
Figure 1: The number of selected columns determines whether the result is a Series or a DataFrame.
5. Select Columns in a Different Order
You can specify column names in any order. Pandas returns the selected columns in the order you provide.
print(df[["Marks", "Name"]])
Output
Marks Name 0 85 Samadhan 1 92 Pradnya 2 78 Samiksha 3 88 Amit
6. Select Columns and Calculate Statistics
After selecting a column, you can apply Series methods such
as mean(), max(), and
min().
print("Average marks:", df["Marks"].mean())
print("Highest marks:", df["Marks"].max())
print("Lowest marks:", df["Marks"].min())
Output
Average marks: 85.75 Highest marks: 92 Lowest marks: 78
Statistics for multiple columns
print(df[["Age", "Marks"]].mean())
Output
Age 21.50 Marks 85.75 dtype: float64
7. Select Columns Using a Variable
If the required columns are stored in a Python list, you can pass that list to the DataFrame.
columns_to_select = ["Name", "City", "Marks"] result = df[columns_to_select] print(result)
Output
Name City Marks 0 Samadhan Pune 85 1 Pradnya Mumbai 92 2 Samiksha Pune 78 3 Amit Nashik 88
This technique is useful when a report or analysis needs different columns depending on the user's choice.
8. Check Available Column Names
Before selecting columns, you can inspect the column names
using df.columns.
print(df.columns)
Output
Index(['Name', 'Age', 'City', 'Marks'], dtype='object')
To get the column names as a regular Python list, use
tolist().
print(df.columns.tolist())
Output
['Name', 'Age', 'City', 'Marks']
9. Visual Guide: Selecting Columns for a Report
Imagine that you want to prepare a marks report. The original DataFrame has four columns, but the report only requires Name and Marks.
Figure 2: Selecting only the required columns creates a focused DataFrame for reporting.
10. Common Errors and Best Practices
Error 1: Incorrect column name
df["marks"]
If the actual column is named Marks, using
marks may raise a KeyError.
Column labels are case-sensitive.
Correct code:
df["Marks"]
Error 2: Forgetting the list for multiple columns
Use a list of names inside the outer brackets:
df[["Name", "Marks"]]
Error 3: Selecting a column that does not exist
Inspect df.columns to check the available
column names before selecting them.
11. Practice Exercises
Exercise 1: Basic Selection
- Select only the Name column.
- Select only the Marks column.
- Select the Name and City columns.
- Select the City and Marks columns.
Exercise 2: Column Order
- Display Marks before Name.
- Display City, Name, and Age in that order.
- Store the selected column names in a list and use that list.
Exercise 3: Calculations
- Calculate the average marks.
- Find the maximum marks.
- Calculate the average Age and Marks together.
- Print all available column names.
Suggested solution
print(df["Name"]) print(df["Marks"]) print(df[["Name", "City"]]) print(df[["City", "Marks"]]) print(df[["Marks", "Name"]]) print(df[["City", "Name", "Age"]]) selected_columns = ["Name", "Marks"] print(df[selected_columns]) print(df["Marks"].mean()) print(df["Marks"].max()) print(df[["Age", "Marks"]].mean()) print(df.columns.tolist())
12. Revision Questions
- What is column selection in Pandas?
- How do you select a single column?
- What is the difference between a Series and a DataFrame?
- How do you select multiple columns?
- How can you change the order of selected columns?
- How do you inspect the column names of a DataFrame?
- What happens if you select a column that does not exist?
- How can you calculate the mean of a selected column?
Summary
Column selection allows you to retrieve only the columns
required for a task. Use df["Name"] to select
a single column and df[["Name", "Marks"]] to
select multiple columns.
A single column selection normally returns a Series, while multiple-column selection returns a DataFrame. You can also store column names in a list, change their display order, inspect available labels, and calculate statistics on selected data.