JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame DataFrame.isin() function

Author:JIYIK Last Updated:2025/04/30 Views:

The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains valuesthe value specified in the input .


pandas.DataFrame.isin(values)grammar

DataFrame.isin(values)

parameter

values iterable- list, tuple, setetc. DictionarySeries DataFrame

Return Value

DataFrameIt 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 Iterableas input

When Python iterableis given as input, the Pandas DataFrame.isin()function checks DataFramewhether each value in contains iterableany 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 DataFrameis

   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. Trueis 100not 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 .valueTrue

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, Profitthe 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 Salesin the column True.


Sample code: DataFrame.isin()Taking Seriesas input

If the input value type is Pandas Series, isin()the function checks if each column element is Seriesthe 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

ProfitThe elements in the column Seriesare the same as the elements in the input , so both elements in the column are returned True.


Sample code: DataFrame.isin()Taking DataFrameas 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.DataFrameDataFrame

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 Falsebecause 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.

Article URL:

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 .

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial