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 October 2025

1 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
$118.60 $259.95
Save 54%
Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)
2 Data Analytics Essentials You Always Wanted To Know : A Practical Guide to Data Analysis Tools and Techniques, Big Data, and Real-World Application for Beginners (Self-Learning Management Series)

Data Analytics Essentials You Always Wanted To Know : A Practical Guide to Data Analysis Tools and Techniques, Big Data, and Real-World Application for Beginners (Self-Learning Management Series)

BUY & SAVE
$29.99 $38.99
Save 23%
Data Analytics Essentials You Always Wanted To Know : A Practical Guide to Data Analysis Tools and Techniques, Big Data, and Real-World Application for Beginners (Self-Learning Management Series)
3 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
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)

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 Univariate, Bivariate, and Multivariate Statistics Using R: Quantitative Tools for Data Analysis and Data Science

Univariate, Bivariate, and Multivariate Statistics Using R: Quantitative Tools for Data Analysis and Data Science

BUY & SAVE
$105.06 $128.95
Save 19%
Univariate, Bivariate, and Multivariate Statistics Using R: Quantitative Tools for Data Analysis and Data Science
6 A PRACTITIONER'S GUIDE TO BUSINESS ANALYTICS: Using Data Analysis Tools to Improve Your Organization’s Decision Making and Strategy

A PRACTITIONER'S GUIDE TO BUSINESS ANALYTICS: Using Data Analysis Tools to Improve Your Organization’s Decision Making and Strategy

  • QUALITY ASSURANCE: EACH BOOK IS CAREFULLY INSPECTED FOR GOOD CONDITION.
  • COST-EFFECTIVE: SAVE MONEY WHILE ENJOYING QUALITY PRE-OWNED READS!
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY CHOOSING A USED BOOK TODAY!
BUY & SAVE
$88.89
A PRACTITIONER'S GUIDE TO BUSINESS ANALYTICS: Using Data Analysis Tools to Improve Your Organization’s Decision Making and Strategy
+
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.