DataFrame.isin() - ( Pandas Data Operations and Processing )
Heading h2
Syntax
DataFrame.isin(values)
Example
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
isin_list = [2, 3]
filtered_df = df[df['A'].isin(isin_list)]
print(filtered_df)
Output
A B
1 2 b
2 3 c
Explanation
DataFrame.isin()
is a method in Pandas that checks whether each element of a DataFrame is contained within a list of values. It returns a boolean DataFrame of the same shape as the original DataFrame, where each element is True
or False
based on whether it is in the list.
In the above example, we create a DataFrame df
with two columns A
and B
. We create a list isin_list
that contains values we want to filter the DataFrame df
with. We then use the df['A'].isin(isin_list)
syntax to filter the rows of df
where the value in column A
is in the isin_list
and store the filtered DataFrame in filtered_df
.
Use
The DataFrame.isin()
method can be used to filter out rows from a DataFrame based on whether their values exist within a list of values.
Important Points
DataFrame.isin()
is a method in Pandas that checks whether each element of a DataFrame is contained within a list of values- It returns a boolean DataFrame of the same shape as the original DataFrame
- Each element is
True
orFalse
based on whether it is in the list - The method can be used to filter out rows from a DataFrame based on whether their values exist within a list of values
Summary
In conclusion, the DataFrame.isin()
method in Pandas is a useful way to filter out rows from a DataFrame based on whether their values exist within a list of values. It returns a boolean DataFrame that can be used to slice the original DataFrame based on the desired condition.