Home >Backend Development >Python Tutorial >How to Subset a Pandas DataFrame Based on a List of Values?

How to Subset a Pandas DataFrame Based on a List of Values?

Barbara Streisand
Barbara StreisandOriginal
2024-12-13 09:42:14560browse

How to Subset a Pandas DataFrame Based on a List of Values?

Subsetting Pandas DataFrames Based on a List of Values

Selecting rows in a Pandas dataframe based on a specific value is straightforward using the equality operator. However, when dealing with multiple values, a more flexible approach is required. This article explains how to subset a dataframe using a list of values.

Problem:

Consider the following dataframe:

df = DataFrame({'A': [5,6,3,4], 'B': [1,2,3,5]})

df

     A   B
0    5   1
1    6   2
2    3   3
3    4   5

We want to select rows where column 'A' matches values in a given list, such as [3, 6]:

list_of_values = [3, 6]

y = df[df['A'] in list_of_values]

Solution:

The isin method of the dataframe provides a convenient way to achieve this:

df[df['A'].isin([3, 6])]

This returns the following rows:

     A    B
1    6    2
2    3    3

For the inverse selection, excluding rows with values in the given list, use the ~ operator:

df[~df['A'].isin([3, 6])]

This returns the remaining rows:

   A  B
0  5  1
3  4  5

Using the isin method, you can easily select or exclude rows based on a list of values, providing a more versatile solution for data extraction from Pandas dataframes.

The above is the detailed content of How to Subset a Pandas DataFrame Based on a List of Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn