📅  最后修改于: 2023-12-03 15:03:28.213000             🧑  作者: Mango
The isin()
method of a Pandas DataFrame is used to check whether each element of the DataFrame is contained in the passed sequence of values. It returns a boolean DataFrame where each element is True
if the corresponding element of the DataFrame is contained in the passed sequence of values and False
otherwise.
DataFrame.isin(values)
values
: list-like or dictTrue
where the DataFrame is contained in the passed sequence of values and False
otherwise.Suppose we have a Pandas DataFrame as shown below:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['a', 'b', 'c'],
'C': [4.0, 5.0, 6.0]
})
df.head()
A B C
0 1 a 4.0
1 2 b 5.0
2 3 c 6.0
Now, we can use the isin()
method to check whether the elements of the DataFrame are contained in a list of values or not. For example, let's check whether the elements of column B are contained in the list ['a', 'c']
:
df['B'].isin(['a', 'c'])
0 True
1 False
2 True
Name: B, dtype: bool
As we can see, the boolean DataFrame returned by the isin()
method has True
for the first and third elements of column B, because they are contained in the passed list [a, c]
.
We can also use the isin()
method on the entire DataFrame by passing a dict with column names and their corresponding values to check for. For example, let's check whether the DataFrame contains any elements from the DataFrame other_df
:
other_df = pd.DataFrame({
'A': [1, 4],
'B': ['c', 'd'],
'C': [7.0, 8.0]
})
df.isin(other_df)
A B C
0 True False False
1 False False False
2 True True False
As we can see, the boolean DataFrame returned by the isin()
method has True
for the elements that are in the other_df
, and False
otherwise. For example, the first row has True
for column A because it contains the element 1
, which is in the other_df
.