Pandas DataFrame DataFrame.isin()関数
-
pandas.DataFrame.isin(values)
の構文 -
コード例:
iterable
を入力とするDataFrame.isin()
-
コード例:
Dictionary
を入力とするDataFrame.isin()
-
コード例:
Series
を入力とするDataFrame.isin()
-
コード例:
DataFrame
を入力とするDataFrame.isin()

pandas.DataFrame.isin(values)
関数は、呼び出し元の DataFrame の各要素に、入力 values
で指定された値が含まれているかどうかを確認します。
pandas.DataFrame.isin(values)
の構文
DataFrame.isin(values)
パラメーター
values |
iterable - list 、tuple 、set などDictionary 、 Series DataFrame |
戻り値
各要素に入力の values
が含まれているかどうかを示す、呼び出し元の DataFrame
と同じ次元のブール値の DataFrame
を返します。
コード例:iterable
を入力とする DataFrame.isin()
Python の iterable
が入力の場合、Pandas の DataFrame.isin()
関数は、DataFrame
の各値に iterable
の値が含まれているかどうかを確認します。
import pandas as pd
df = pd.DataFrame({'Sales': [100, 200], 'Profit': [200, 400]})
df = df.isin([200, 400])
print(df)
呼び出し元の DataFrame
は
Sales Profit
0 100 200
1 200 400
出力:
Sales Profit
0 False True
1 True True
ここでは、リスト [200、400]
に 200 と 400 が存在するため、元の値が 200 と 400 である返された DataFrame
の値は True
です。100
はリスト [200、400]
にないため、その位置の値は False
を返します。
コード例:Dictionary
を入力とする DataFrame.isin()
入力値のタイプが辞書
の場合、isin()
関数は値だけでなくキー
もチェックします。列名が key
と同じで、セルの値が辞書の value
に含まれている場合にのみ True
を返します。
import pandas as pd
df = pd.DataFrame({'Sales': [100, 200], 'Profit': [200, 400]})
df = df.isin({'Sales': [200, 400]})
print(df)
出力:
Sales Profit
0 False False
1 True False
最初の例では、Profit
列の値はどちらも True
ですが、この例では列名が入力ディクショナリの key
と異なるため、False
です。
ディクショナリの value
に値が含まれている場合、Sales
列に True
を返します- [200、400]
。
コード例:Series
を入力とする DataFrame.isin()
入力値のタイプが Pandas Series
の場合、isin()
関数は列ごとの要素が入力 Series
の同じインデックスの値と同じかどうかを確認します。
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)
出力:
0 200
1 400
dtype: int64
Sales Profit
0 False True
1 False True
Profit
列の要素は入力の Series
要素の要素と同じであるため、その列の両方の要素に対して True
を返します。
コード例:DataFrame
を入力とする DataFrame.isin()
入力値のタイプが Pandas DataFrame
の場合、isin()
関数は呼び出し元の DataFrame
の各要素が同じ位置にある入力 DataFrame
の要素と同じかどうかを確認します。
値が同一の場合は True
を返し、不一致の場合は 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)
出力:
Sales Profit
0 100 200
1 200 400
Sales Profit
0 100 200
1 200 300
Sales Profit
0 True True
1 True False
呼び出し元の DataFrame と入力の DataFrame の間で値が異なるため、位置 (1、1)
の値は False
を返します。
isin()
関数は値を要素ごとにチェックするだけでなく、列の名前が同一かどうかもチェックします。これらの 2つの DataFrame
で値が同じであっても、列名が異なる場合は False
を返します。Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn関連記事 - Pandas DataFrame
- Pandas cut 関数
- Pandas DataFrame sort_index() 関数
- Pandas DataFrame.idxmax() 関数
- Pandas DataFrame.insert() 関数
- Pandas DataFrame.resample() 関数
- Pandas DataFrame.reset_index() 関数