Learn How to Select Specific Columns in Pandas DataFrames

Understanding Column Subsetting in Pandas

In the world of Pandas library, working with large datasets often requires analysts and data scientists to focus only on a specific subset of features or variables. This process, known as data subsetting, is crucial for improving computation speed, conserving memory, and ensuring that subsequent analyses or machine learning models only process relevant information. When dealing with a DataFrame, selecting or filtering columns is one of the most fundamental operations.

There are two primary strategies for achieving this objective. The first, and most commonly used, is the inclusion method: explicitly stating which columns you wish to retain. The second strategy is exclusion: explicitly stating which columns you wish to discard. Depending on whether you have a short list of columns to keep or a short list of columns to drop, one method will prove significantly more efficient and readable than the other.

Mastering the syntax for column selection in Pandas is vital. Unlike selecting rows, which often involves slicing or conditional logic, column selection typically relies on passing a list of column identifiers to the indexing operator. Below, we introduce the foundational methods that allow you to efficiently manage the dimensional structure of your DataFrame, followed by detailed, practical examples.

Core Techniques for Column Selection

The following powerful techniques allow you to precisely control which columns remain in your resulting DataFrame. These methods are designed to be concise and highly performant, which is especially important when processing millions of rows of data.


Method 1: Specify Columns to Keep

This method involves using the double bracket notation ([[...]]) to pass a list of column names directly to the DataFrame indexer. This is the recommended approach when you know exactly which columns you need for your analysis, and that list is relatively short compared to the total number of columns.

#only keep columns 'col1' and 'col2'
df[['col1', 'col2']]

It is crucial to use the double square brackets. The outer brackets perform the selection operation on the DataFrame object, while the inner brackets define the Python list containing the names of the desired columns. Failure to include the inner list will result in a Pandas error, as the indexer expects an iterable when selecting multiple columns.


Method 2: Specify Columns to Drop (Using Boolean Indexing)

While the most intuitive way to drop columns might be using the .drop() method, another highly effective and memory-efficient strategy involves utilizing boolean mask indexing in conjunction with the .isin() function. This technique is particularly useful when you need to exclude a few specific columns and retain the vast majority.

#drop columns 'col3' and 'col4'
df[df.columns[~df.columns.isin(['col3', 'col4'])]]

This syntax works by first generating a boolean series that checks if each column name is present in the list of columns to be dropped (using .isin()). The tilde symbol (~) then acts as a negation operator, flipping the mask. This means that columns listed inside .isin() evaluate to False (drop), and all other columns evaluate to True (keep). This resulting boolean array is then applied to the DataFrame’s column index, effectively selecting only the columns that were marked True.

Setting Up the Sample DataFrame

To illustrate these techniques clearly, we will first construct a sample DataFrame containing fictional sports statistics. This setup ensures that we have a concrete object to manipulate and test the column selection methods described above.

We begin by importing the Pandas library, which is standard practice in any Python data manipulation workflow. Our sample data structure includes four core columns: team, points, assists, and rebounds.

The following code initializes the DataFrame and provides a clean view of the data structure we will be working with throughout the subsequent examples.

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B'],
                   'points': [11, 7, 8, 10, 13, 13],
                   'assists': [5, 7, 7, 9, 12, 9],
                   'rebounds': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

	team	points	assists	rebounds
0	A	11	5	11
1	A	7	7	8
2	A	8	7	10
3	B	10	9	6
4	B	13	12	6
5	B	13	9	5

Practical Implementation: Selecting Specific Columns (Example 1)

In this first practical application, we demonstrate Method 1 by creating a new DataFrame that contains only the core statistical information: the team identifier and the points scored. We deliberately exclude the assists and rebounds columns from the output.

The code below uses the straightforward list-of-names indexing technique. This method is highly transparent and easy to debug, making it the preferred choice for analysts who want immediate control over their output structure. We assign the result to a new variable, df2, ensuring that the original DataFrame, df, remains unchanged.

Observe how the double bracket notation handles the subsetting operation. The resulting DataFrame is a perfect subset of the original, maintaining all the rows but reducing the feature space to just the two columns we requested.

#create new DataFrame and only keep 'team' and 'points' columns
df2 = df[['team', 'points']]

#view new DataFrame
df2

        team	points
0	A	11
1	A	7
2	A	8
3	B	10
4	B	13
5	B	13

As expected, the resulting DataFrame df2 successfully retained only the team and points columns, discarding the other metrics present in the original dataset.

Practical Implementation: Dropping Columns via Boolean Masking (Example 2)

For our second example, we utilize Method 2 to achieve the identical result—a DataFrame containing only team and points—but this time, we define the operation by specifying the columns we want to drop: assists and rebounds. This showcases the flexibility of Pandas indexing logic.

This approach leverages advanced indexing features. We first identify the names of the columns we wish to drop using df.columns.isin(['assists', 'rebounds']). This yields a boolean series (e.g., [False, False, True, True]). By applying the negation operator (~), we reverse this series to [True, True, False, False], which serves as a precise mask to select the columns we intend to keep.

This technique is highly valuable in scenarios where a dataset contains dozens of features, but only three or four need to be ignored. Instead of listing 30 columns to keep, you only list the 4 columns to drop, dramatically simplifying the code and reducing the chance of typographical errors.

#create new DataFrame and that drops 'assists' and 'rebounds'
df2 = df[df.columns[~df.columns.isin(['assists', 'rebounds'])]]

#view new DataFrame
df2

        team	points
0	A	11
1	A	7
2	A	8
3	B	10
4	B	13
5	B	13

The output confirms that df2 now only contains the team and points columns, demonstrating the successful application of the boolean masking logic to exclude the specified variables from the original dataset.

Conclusion and Advanced Considerations

Effectively managing columns is central to efficient data processing using Pandas. We have explored two robust methods for achieving column subsetting: the explicit selection of columns to keep (Method 1) and the powerful exclusion technique using boolean indexing (Method 2). While Method 1 is highly readable for small subsets, Method 2 offers superior efficiency and code conciseness when the goal is to drop only a handful of columns from a vast DataFrame.

For advanced users, it is also worth noting that Pandas offers alternative methods, such as using the .drop() function with the axis=1 argument, or selecting columns based on data types (e.g., keeping only numerical columns). However, for simple selection tasks, the two methods detailed in this article provide the most direct and idiomatic Python solutions.

By mastering these fundamental indexing and selection techniques, you ensure that your data preparation phase is streamlined, setting a strong foundation for any subsequent analytical tasks. Always choose the method that best balances clarity, maintainability, and execution speed for your specific analytical requirements.

Additional Resources

To continue developing your skills in data manipulation using Pandas, the following tutorials explain how to perform other common operations:

  • Filtering Rows: Learn how to subset data based on conditional criteria applied to row values.
  • Handling Missing Data: Explore techniques for identifying, dropping, or imputing missing values (NaNs).
  • Grouping and Aggregation: Understand how to use the .groupby() function for statistical summarization.

Cite this article

Mohammed looti (2025). Learn How to Select Specific Columns in Pandas DataFrames. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/keep-certain-columns-in-pandas-with-examples/

Mohammed looti. "Learn How to Select Specific Columns in Pandas DataFrames." PSYCHOLOGICAL STATISTICS, 1 Nov. 2025, https://statistics.arabpsychology.com/keep-certain-columns-in-pandas-with-examples/.

Mohammed looti. "Learn How to Select Specific Columns in Pandas DataFrames." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/keep-certain-columns-in-pandas-with-examples/.

Mohammed looti (2025) 'Learn How to Select Specific Columns in Pandas DataFrames', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/keep-certain-columns-in-pandas-with-examples/.

[1] Mohammed looti, "Learn How to Select Specific Columns in Pandas DataFrames," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learn How to Select Specific Columns in Pandas DataFrames. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top