LoginSignup
0
2

More than 1 year has passed since last update.

Seriesのフィルタリング

Posted at

概要

条件に一致する要素を取り出したい場合、bool型のシーケンスを指定すると、Trueとなるものだけを抽出できる。

IN

import pandas as pd

fruits = ["apple", "orange", "banana", "straberry", "kiwifruit"]
data = [10, 5, 8, 12, 3]
#Seriesの生成
series = pd.Series(data, index = fruits)
print(series)

#bool型のシーケンスを指定
f = [True, True, False, False, False]

print(series[f])

out

image.png

Trueのみ抽出できました。

条件式の利用

上記ではbool型でシーケンスを作成したが、PandasではSeriesやDataFrameを使って条件式(変数に対して,変数[条件式])を作成するとbool型のシーケンスを取得する事ができる。
さらに、変数[ ][ ]のように[ ]を複数後ろに付け加えると、複数の条件を指定する事が可能になる。ただし、変数[ ~ and ~ ]という形はできない

#条件式を利用
print(series[series >= 8])

image.png

条件式によりデータ値が8以上のもののみ抽出できました。

0
2
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
0
2