pandas:Seriesから比較演算結果(条件)を指定してデータを取得する方法

スポンサーリンク

Seriesから比較演算結果(条件)を指定してデータを取得する方法

Seriesから比較演算結果(条件)を指定してデータを取得するには以下のようにします。

import pandas as pd

ser = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
print(ser)
# a 1
# b 2
# c 3
# d 4
# e 5
# dtype: int64

print(ser != 2)
# a True
# b False
# c True
# d True
# e True
# dtype: bool
print(ser.loc[ser != 2])
# a 1
# c 3
# d 4
# e 5
# dtype: int64

print(ser % 2 == 0)
# a False
# b True
# c False
# d True
# e False
# dtype: bool
print(ser.loc[ser % 2 == 0])
# b 2
# d 4
# dtype: int64
pandas:Seriesから真偽値(True, False)を指定してデータを取得する方法
Seriesから真偽値(True, False)を指定してデータを取得する方法 Seriesから真偽値(True, False)を指定してデータを取得するには以下のようにします。 import pandas as pd ser = pd.S...