Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.isin#

DataFrame.isin(values)[source]#

Whether each element in the DataFrame is contained in values.

Parameters:
valuesiterable, Series, DataFrame or dict

The result will only be true at a location if all thelabels match. Ifvalues is a Series, that’s the index. Ifvalues is a dict, the keys must be the column names,which must match. Ifvalues is a DataFrame,then both the index and column labels must match.

Returns:
DataFrame

DataFrame of booleans showing whether each element in the DataFrameis contained in values.

See also

DataFrame.eq

Equality test for DataFrame.

Series.isin

Equivalent method on Series.

Series.str.contains

Test if pattern or regex is contained within a string of a Series or Index.

Examples

>>>df=pd.DataFrame({'num_legs':[2,4],'num_wings':[2,0]},...index=['falcon','dog'])>>>df        num_legs  num_wingsfalcon         2          2dog            4          0

Whenvalues is a list check whether every value in the DataFrameis present in the list (which animals have 0 or 2 legs or wings)

>>>df.isin([0,2])        num_legs  num_wingsfalcon      True       Truedog        False       True

To check ifvalues isnot in the DataFrame, use the~ operator:

>>>~df.isin([0,2])        num_legs  num_wingsfalcon     False      Falsedog         True      False

Whenvalues is a dict, we can pass values to check for eachcolumn separately:

>>>df.isin({'num_wings':[0,3]})        num_legs  num_wingsfalcon     False      Falsedog        False       True

Whenvalues is a Series or DataFrame the index and column mustmatch. Note that ‘falcon’ does not match based on the number of legsin other.

>>>other=pd.DataFrame({'num_legs':[8,3],'num_wings':[0,2]},...index=['spider','falcon'])>>>df.isin(other)        num_legs  num_wingsfalcon     False       Truedog        False      False

[8]ページ先頭

©2009-2025 Movatter.jp