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

How to Subset a Pandas DataFrame Based on Multiple Values?

Barbara Streisand
Release: 2024-12-16 20:37:24
Original
210 people have browsed it

How to Subset a Pandas DataFrame Based on Multiple Values?

Subsetting a Pandas DataFrame Based on Multiple Values

In Pandas, subsetting a DataFrame based on a specific value is straightforward, as demonstrated by the following example:

import pandas as pd

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

# Subset based on a single value
x = df[df['A'] == 3]
Copy after login

However, the challenge arises when selecting rows that match a list of values. Consider the following use case:

# List of values to filter on
list_of_values = [3, 6]

# Subset attempt (incorrect syntax)
y = df[df['A'] in list_of_values]
Copy after login

This syntax will result in an error, as Pandas requires a slightly different syntax to subset based on multiple values.

Solution: Using the isin() Method

The correct way to subset a DataFrame based on a list of values is to use the isin() method. Here's the corrected code:

y = df[df['A'].isin(list_of_values)]
Copy after login

Output:

     A  B
1    6  2
2    3  3
Copy after login

The isin() method takes a list or array of values as input and returns a DataFrame containing rows where the specified column matches any value in the input.

Inverse Selection

To select rows where the column values do not match the supplied list, you can use the ~ operator in conjunction with isin(). For example:

# Inverse subset
z = df[~df['A'].isin(list_of_values)]
Copy after login

Output:

   A  B
0  5  1
3  4  5
Copy after login

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template