In data analysis, it's often necessary to retrieve specific rows from a dataframe based on predefined criteria. Pandas provides various methods for subsetting dataframes, including the ability to select rows based on a list of values.
To subset a Pandas dataframe based on a list of values, you can employ the isin() method, as demonstrated below:
import pandas as pd # Create a Pandas dataframe df = pd.DataFrame({'A': [5, 6, 3, 4], 'B': [1, 2, 3, 5]}) # Define a list of values to filter by list_of_values = [3, 6] # Subset dataframe based on the list y = df[df['A'].isin(list_of_values)] print(y)
Output:
A B 1 6 2 2 3 3
The isin() method allows you to filter rows where the specified column values match any value in the provided list.
In certain scenarios, you may need to exclude rows based on the list of values. To achieve this, you can use the ~ operator along with isin(), as illustrated below:
import pandas as pd # Create a Pandas dataframe df = pd.DataFrame({'A': [5, 6, 3, 4], 'B': [1, 2, 3, 5]}) # Define a list of values to exclude list_of_values = [3, 6] # Subset dataframe excluding the list z = df[~df['A'].isin(list_of_values)] print(z)
Output:
A B 0 5 1 3 4 5
The ~ operator negates the selection, ensuring that rows with values not in the specified list are displayed.
The above is the detailed content of How to Subset Pandas DataFrames Using a List of Values?. For more information, please follow other related articles on the PHP Chinese website!