Skip to main content
TopMiniSite

Back to all posts

How to Select Specific Columns In A Pandas DataFrame?

Published on
4 min read
How to Select Specific Columns In A Pandas DataFrame? image

Best Data Analysis Tools to Buy in November 2025

1 Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

BUY & SAVE
$43.99 $79.99
Save 45%
Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter
2 Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists

Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists

BUY & SAVE
$14.01 $39.99
Save 65%
Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists
3 Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)

Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)

BUY & SAVE
$81.77 $259.95
Save 69%
Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)
4 Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)

Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)

  • UNMATCHED QUALITY: BUILT TO LAST, ENSURING CUSTOMER SATISFACTION AND TRUST.
  • COMPETITIVE PRICING: GET PREMIUM FEATURES WITHOUT BREAKING THE BANK.
  • EXCEPTIONAL SUPPORT: 24/7 ASSISTANCE TO ENHANCE USER EXPERIENCE AND LOYALTY.
BUY & SAVE
$29.95 $37.95
Save 21%
Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)
5 Data Analysis with LLMs: Text, tables, images and sound (In Action)

Data Analysis with LLMs: Text, tables, images and sound (In Action)

BUY & SAVE
$38.39
Data Analysis with LLMs: Text, tables, images and sound (In Action)
6 Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions

Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions

BUY & SAVE
$29.61 $59.99
Save 51%
Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions
+
ONE MORE?

To select specific columns in a Pandas DataFrame, you can use the square bracket notation or the dot notation. Here's how you can do it:

  1. Square Bracket Notation: You can use the square bracket notation by passing a list of column names as an argument. This method returns a new DataFrame containing only the specified columns. Example: df_new = df[['column1', 'column2']]
  2. Dot Notation: If your column names are valid Python variable names (i.e., without spaces or special characters), you can also use the dot notation to access specific columns. This method is often more concise and readable. However, it is important to note that dot notation won't work if your column names contain spaces or special characters. Example: df_new = df.column1

These methods allow you to select and work with only the desired subset of columns from a Pandas DataFrame, making data manipulation and analysis more focused and efficient.

How to select columns based on their position in a Pandas DataFrame?

To select columns based on their position in a Pandas DataFrame, you can use the .iloc indexer.

Here's how you can do it:

  1. Import the Pandas library:

import pandas as pd

  1. Create a DataFrame:

data = {'col1': [1, 2, 3, 4, 5], 'col2': [6, 7, 8, 9, 10], 'col3': [11, 12, 13, 14, 15]} df = pd.DataFrame(data)

  1. Use .iloc to select columns based on their position:

selected_columns = df.iloc[:, [0, 2]] # Selects columns at position 0 and 2

In the example above, selected_columns will be a new DataFrame that contains only the columns at positions 0 and 2 (in this case, 'col1' and 'col3').

Note: The first colon (:) in df.iloc[:, [0, 2]] selects all rows, and [0, 2] selects columns at positions 0 and 2 (you can modify this to select columns at different positions).

How to select columns from a Pandas DataFrame using a list of column names?

To select columns from a pandas DataFrame using a list of column names, you can follow these steps:

  1. Import the pandas library:

import pandas as pd

  1. Create a dictionary or a pandas DataFrame:

data = {'column1': [1, 2, 3], 'column2': [4, 5, 6], 'column3': [7, 8, 9]} df = pd.DataFrame(data)

  1. Create a list of column names you want to select:

columns_to_select = ['column1', 'column3']

  1. Use the loc accessor with the list of column names to select the desired columns:

selected_columns = df.loc[:, columns_to_select]

Now, selected_columns will be a new DataFrame containing only the columns specified in the list columns_to_select. It will have the same rows as the original DataFrame but only include the selected columns.

How to select columns with a specific range of values in a Pandas DataFrame?

To select columns with a specific range of values in a Pandas DataFrame, you can use boolean indexing. Here's an example of how to do it:

import pandas as pd

Create sample DataFrame

data = {'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50], 'C': [100, 200, 300, 400, 500]}

df = pd.DataFrame(data)

Select columns with values between 2 and 4

selected_columns = df.loc[:, (df >= 2) & (df <= 4)]

print(selected_columns)

Output:

A B C 0 NaN NaN NaN 1 2.0 NaN NaN 2 3.0 NaN NaN 3 4.0 NaN NaN 4 NaN NaN NaN

In the example above, the loc method is used to select all rows (:) and columns where the values are between 2 and 4 ((df >= 2) & (df <= 4)). The resulting DataFrame selected_columns contains only the columns with values between 2 and 4, while columns with values outside this range are filled with NaN.