26
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pandas データフレーム検索 isin メソッド

Posted at

データフレームの列に値が含まれているかどうかをチェックする方法を毎度忘れる。
isin メソッドがシンプルで使いやすいと思うが、日本語の記事はなかなか検索でヒットしないため、メモ。

1、df[X].isin([Y])
isin内の値を単独のスカラー量Yのようにするとエラーとなる。
このメソッドは引数にList like objectしか受け付けないとのこと。
スカラー量Yを[Y]としてリストにすると実行される。

2、df[X].isin([Y]).any()
any()をつけるとその列全体で存在するかどうかをBool型で返してくれる。便利。

参考リンク
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.isin.html

26
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?