Sling Academy
Home/Pandas/Pandas: How to select multiple columns from a DataFrame

Pandas: How to select multiple columns from a DataFrame

Last updated: February 20, 2024

Introduction

Pandas, a prominent data manipulation library in Python, simplifies data analysis through its powerful DataFrame object. A common task in data analysis involves selecting specific columns from a DataFrame for further processing. This tutorial aims to provide a comprehensive guide on various methods to select multiple columns, catering to different scenarios and preferences. By the end of this tutorial, you will be well-equipped to select columns efficiently in your data analysis projects.

Getting Started

Before diving into the methods of selecting columns, ensure you have Pandas installed:

pip install pandas

Let’s create a sample DataFrame to work with through this tutorial:

import pandas as pd

data = {
    "name": ["Alice", "Bob", "Charlie"],
    "age": [25, 30, 35],
    "city": ["New York", "Paris", "London"],
}
df = pd.DataFrame(data)
print(df)

The df DataFrame looks like this:

      name  age      city
0    Alice   25  New York
1      Bob   30     Paris
2  Charlie   35    London

Basic Selection

To start with, selecting columns in Pandas can be achieved through direct indexing. If you want to select the 'name' and 'age' columns, you can do it as follows:

result = df[['name', 'age']]
print(result)

Output:

      name  age
0    Alice   25
1      Bob   30
2  Charlie   35

This method is straightforward and works well for most basic needs.

Loc and Iloc Methods

For more advanced selection, loc and iloc methods come into play. The loc method is used for label-based indexing, whereas iloc is for position-based indexing.

Selecting 'name' and 'city' columns using loc:

result = df.loc[:, ['name', 'city']]
print(result)

Output:

      name      city
0    Alice  New York
1      Bob     Paris
2  Charlie    London

And using iloc to select the first and third columns:

result = df.iloc[:, [0, 2]]
print(result)

Output (the same as above):

     name      city
0    Alice  New York
1      Bob     Paris
2  Charlie    London

Both methods provide greater control over the columns to be selected, allowing for more complex data manipulation tasks.

Using Boolean Conditions

Boolean indexing can be leveraged to select columns based on specific conditions. Suppose you want to select columns where the average value is above a certain threshold. While slightly more involved, this method can offer dynamic column selection capabilities.

mean_values = df.mean(numeric_only=True)
selected_columns = mean_values[mean_values > 27].index
df[selected_columns]

This method evaluates which columns meet the condition and selects them accordingly.

Column Selection Using Regular Expressions

Regular expressions are a powerful tool for pattern matching in strings. Pandas allows the use of regular expressions for selecting columns by their names.

df.filter(regex='^c')

This command will select all columns whose names start with the letter ‘c’. This method is particularly useful when dealing with large datasets with similarly named variables.

Mixing Different Methods

Often, the need arises to combine multiple selection strategies for complex data manipulation tasks. Pandas’ flexibility accommodates such needs seamlessly. For example, using loc in conjunction with boolean indexing enables highly tailored column selection:

df.loc[:, df.columns[df.sum() > 100]]

This command selects columns based on the sum of their values, showcasing Pandas’ versatility.

Conclusion

Selecting multiple columns in Pandas is a fundamental skill with numerous methods tailored to various scenarios and preferences. By understanding and combining these methods, you can perform efficient data manipulation to support robust data analysis pipelines. As you become more familiar with these techniques, you’ll find that selecting columns in Pandas becomes a seamless part of your data analysis workflow.

Next Article: Pandas: How to swap 2 columns in a DataFrame

Previous Article: Pandas DataFrame: How to select rows based on column values

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)