How to Select Multiple Columns in a Pandas Dataframe

Avatar

By squashlabs, Last Updated: Oct. 17, 2023

How to Select Multiple Columns in a Pandas Dataframe

To select multiple columns in a pandas dataframe, you can use various techniques and methods provided by the pandas library in Python. In this answer, we will explore some of the commonly used methods for selecting multiple columns in a pandas dataframe.

Method 1: Using Bracket Notation

One of the simplest and most commonly used methods to select multiple columns in a pandas dataframe is by using bracket notation. You can pass a list of column names inside the brackets to select those specific columns.

Here's an example:

import pandas as pd

# Create a sample dataframe
data = {'Name': ['John', 'Jane', 'Mike', 'Emily'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'London', 'Paris', 'Tokyo'],
        'Salary': [50000, 60000, 70000, 80000]}

df = pd.DataFrame(data)

# Select multiple columns using bracket notation
selected_columns = df[['Name', 'Age', 'Salary']]

print(selected_columns)

Output:

   Name  Age  Salary
0  John   25   50000
1  Jane   30   60000
2  Mike   35   70000
3  Emily  40   80000

In the above example, we created a dataframe with four columns: 'Name', 'Age', 'City', and 'Salary'. We then used the bracket notation to select the 'Name', 'Age', and 'Salary' columns. The resulting dataframe selected_columns contains only those selected columns.

Related Article: How to Handle Nonetype Objects in Python

Method 2: Using the loc[] method

Another method to select multiple columns in a pandas dataframe is by using the loc[] method. The loc[] method allows you to select rows and columns based on labels.

Here's an example:

import pandas as pd

# Create a sample dataframe
data = {'Name': ['John', 'Jane', 'Mike', 'Emily'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'London', 'Paris', 'Tokyo'],
        'Salary': [50000, 60000, 70000, 80000]}

df = pd.DataFrame(data)

# Select multiple columns using loc[]
selected_columns = df.loc[:, ['Name', 'Age', 'Salary']]

print(selected_columns)

Output:

   Name  Age  Salary
0  John   25   50000
1  Jane   30   60000
2  Mike   35   70000
3  Emily  40   80000

In the above example, we used the loc[] method with the : operator to select all rows and the list ['Name', 'Age', 'Salary'] to select the desired columns. The resulting dataframe selected_columns contains only those selected columns.

Method 3: Using the iloc[] method

The iloc[] method is similar to the loc[] method, but instead of using labels, it uses integer-based indexing to select rows and columns. You can use the integer-based column indices to select multiple columns in a pandas dataframe.

Here's an example:

import pandas as pd

# Create a sample dataframe
data = {'Name': ['John', 'Jane', 'Mike', 'Emily'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'London', 'Paris', 'Tokyo'],
        'Salary': [50000, 60000, 70000, 80000]}

df = pd.DataFrame(data)

# Select multiple columns using iloc[]
selected_columns = df.iloc[:, [0, 1, 3]]

print(selected_columns)

Output:

   Name  Age  Salary
0  John   25   50000
1  Jane   30   60000
2  Mike   35   70000
3  Emily  40   80000

In the above example, we used the iloc[] method with the : operator to select all rows and the list [0, 1, 3] to select the columns at positions 0, 1, and 3. The resulting dataframe selected_columns contains only those selected columns.

Best Practices and Additional Tips

- When selecting multiple columns using the bracket notation or the loc[] method, make sure to pass the column names as a list.

- The order of the columns in the selected dataframe will be the same as the order of the column names in the list.

- If you want to select consecutive columns, you can use the : operator with the column indices in the iloc[] method. For example, df.iloc[:, 0:3] will select columns 0, 1, and 2.

- If you want to select all columns except a few, you can use the drop() method. For example, df.drop(['Column1', 'Column2'], axis=1) will drop columns 'Column1' and 'Column2' from the dataframe.

These are some of the commonly used methods for selecting multiple columns in a pandas dataframe. You can choose the method that best suits your needs and coding style.

More Articles from the How to do Data Analysis with Python & Pandas series:

How to Integrate Python with MySQL for Database Queries

Pair Python with MySQL to execute database queries effortlessly. Learn about the Python MySQL Connector, establishing connections, interacting with M… read more

How to Pip Install From a Git Repo Branch

Guide on executing pip install from a specific Git Repo Branch in Python. This article provides step-by-step instructions on how to install packages … read more

How to do Matrix Multiplications in Numpy

Perform matrix multiplication effortlessly using Numpy in Python. This article introduces you to the concept of matrix multiplication and guides you … read more

How To Replace Text with Regex In Python

Learn how to use Python to replace regex patterns in strings with simple examples and step-by-step instructions. Discover how to use re.sub() to easi… read more

Python Bitwise Operators Tutorial

Learn how to use Python bitwise operators with this tutorial. From understanding the basic operators like AND, OR, XOR, and NOT, to exploring advance… read more

Fixing "ValueError: Setting Array with a Sequenc" In Python

When working with arrays in Python, you may encounter the "ValueError: setting an array element with a sequence" error. This article provides solutio… read more

How to Structure Unstructured Data with Python

In this article, you will learn how to structure unstructured data using the Python programming language. We will explore the importance of structuri… read more

Python Deleter Tutorial

The Python deleter is a powerful tool that allows you to efficiently remove files, directories, and specific elements from lists and dictionaries in … read more

Comparing Substrings in Python

This technical guide provides an overview of substring comparison in Python, covering various methods such as using index, slice, substring function,… read more

How to Determine the Length of an Array in Python

This article provides a step-by-step guide on how to measure the length of an array in Python. It covers an overview of length functions in Python, u… read more