Pandas DataFrame DataFrame.isin() function
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values
the value specified in the input .
pandas.DataFrame.isin(values)
grammar
DataFrame.isin(values)
parameter
values |
iterable - list , tuple , set etc. DictionarySeries DataFrame |
Return Value
DataFrame
It returns a boolean value of the same dimension as the caller DataFrame
, indicating whether each element contains the input values
.
Sample code: DataFrame.isin()
Taking Iterable
as input
When Python iterable
is given as input, the Pandas DataFrame.isin()
function checks DataFrame
whether each value in contains iterable
any of the values in .
import pandas as pd
df = pd.DataFrame({'Sales': [100, 200], 'Profit': [200, 400]})
df = df.isin([200, 400])
print(df)
The caller DataFrame
is
Sales Profit
0 100 200
1 200 400
Output:
Sales Profit
0 False True
1 True True
Here, 200 and 400 are present [200,400]
in the list , so in the returned DataFrame
, the original values are 200 and 400. True
is 100
not in the list [200,400]
, so the value in its place is returned False
.
Example code: DataFrame.isin()
Taking a dictionary as input
If the input value type is dictionary, the function checks not only the value but also the key value. It returns isin()
only if the column name is the same as the key and the cell value is contained in the dictionary .value
True
import pandas as pd
df = pd.DataFrame({"Sales": [100, 200], "Profit": [200, 400]})
df = df.isin({"Sales": [200, 400]})
print(df)
Output:
Sales Profit
0 False False
1 True False
In the first example, Profit
the values of the columns are all True
, but in this case they are False
, because the column names are different than the keys in the input dictionary.
If the value is contained in the dictionary – [200,400]
it will return the value Sales
in the column True
.
Sample code: DataFrame.isin()
Taking Series
as input
If the input value type is Pandas Series
, isin()
the function checks if each column element is Series
the same as the value in the same index of the input .
import pandas as pd
df = pd.DataFrame({"Sales": [100, 200], "Profit": [200, 400]})
valueSeries = pd.Series([200, 400])
print(valueSeries)
df = df.isin(valueSeries)
print(df)
Output:
0 200
1 400
dtype: int64
Sales Profit
0 False True
1 False True
Profit
The elements in the column Series
are the same as the elements in the input , so both elements in the column are returned True
.
Sample code: DataFrame.isin()
Taking DataFrame
as input
If the input value type is Pandas, the function checks whether each element in DataFrame.isin()
the caller is the same as the element at the same position in the input.DataFrame
DataFrame
Returns if the values are the same True
, otherwise returns None False
.
import pandas as pd
df = pd.DataFrame({"Sales": [100, 200], "Profit": [200, 400]})
print(df)
valueDf = pd.DataFrame({"Sales": [100, 200], "Profit": [200, 300]})
print(valueDf)
df = df.isin(valueDf)
print(df)
Output:
Sales Profit
0 100 200
1 200 400
Sales Profit
0 100 200
1 200 300
Sales Profit
0 True True
1 True False
(1, 1)
The value of position is returned False
because the values of the caller DataFrame and the input DataFrame are different.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
How to Apply a Function to a Column in a Pandas Dataframe
Publish Date:2025/04/30 Views:50 Category:Python
-
In Pandas, you can transform and manipulate columns and DataFrames using methods apply() such as transform() and . The desired transformation is passed to these methods as a function argument. Each method has its own subtle differences and
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:124 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .