0
0
Pandasdata~5 mins

Selecting columns by name in Pandas

Choose your learning style9 modes available
Introduction

We select columns by name to focus on specific data we want to analyze or use. It helps us work only with the information we need.

You want to see only the 'age' and 'salary' columns from a large employee dataset.
You need to create a new table with just the 'name' and 'email' columns for sending newsletters.
You want to analyze sales data but only for the 'region' and 'sales_amount' columns.
You want to remove unnecessary columns before saving a smaller file.
You want to quickly check values in a specific column like 'date' or 'status'.
Syntax
Pandas
df['column_name']
df[['col1', 'col2']]
# df is your DataFrame

Use single brackets [] for one column, double brackets [[]] for multiple columns.

The result for one column is a Series; for multiple columns, it's a DataFrame.

Examples
Selects the column named 'age' as a Series.
Pandas
df['age']
Selects two columns 'name' and 'email' as a new DataFrame.
Pandas
df[['name', 'email']]
Stores selected columns in a new variable and prints the first 5 rows.
Pandas
selected = df[['region', 'sales_amount']]
print(selected.head())
Sample Program

This code creates a small table with people data. It then selects one column 'age' and prints it. Next, it selects two columns 'name' and 'email' and prints that smaller table.

Pandas
import pandas as pd

# Create a sample DataFrame
data = {
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35],
    'email': ['alice@example.com', 'bob@example.com', 'charlie@example.com'],
    'salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)

# Select the 'age' column
age_column = df['age']
print('Age column:')
print(age_column)

# Select multiple columns 'name' and 'email'
contact_info = df[['name', 'email']]
print('\nContact info DataFrame:')
print(contact_info)
OutputSuccess
Important Notes

If you try to select a column name that does not exist, pandas will give an error.

Using double brackets always returns a DataFrame, which is useful if you want to keep the table format.

Summary

Select columns by their names to work with specific data.

Use single brackets [] for one column (returns Series).

Use double brackets [[]] for multiple columns (returns DataFrame).