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!