pandas~要素編~
前書き
今回は、要素の一意性や頻度、存在の確認方法について説明します。
一意な要素
一意な要素を求めるメソッドは、以下のものです。
- Series.unique() : Series中の一意な要素を求める。返り値は、list。
- DataFrame.nunique() : DataFrameの中の一意な要素の個数を求める。返り値は、Series。
# sample.py import pandas as pd s = pd.Series([1, 2, 3, 5, 8, 2, 4, 5, 3, 4, 5, 1]) print(s.unique()) d = pd.DataFrame([[1, 'a'], [2, 'a'], [3, 'c'], [5, 'c'], [2, 'a'], [2, 'd'], [4, 'd'], [5, 'c'], [3, 'c'], [4, 'd'], [5, 'c'], [1, 'a']]) print(d.nunique()) print(d.nunique(axis=1)) print(d.iloc[:, 0].unique()) # 0列目(Series型)を取り出し、unique()を使用
# 実行結果 # s.unique() array([1, 2, 3, 5, 8, 4]) # d.nunique() 0 5 1 3 dtype: int64 # d.unique(axis=1) 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 dtype: int64 # d.iloc[:, 0].unique() array([1, 2, 3, 5, 4])
上記の実行結果になります。DataFrameのnunique()はあまり使われません。最後の例のように、DataFrameは、一意な要素を求めたいSeriesを取り出してからunique()を使うことが多いです。
要素の頻度
要素の頻度を求めるメソッドは、以下のものです。
- Series.value_counts() : Series中の要素の頻度を求める。返り値は、Series。
# sample.py import pandas as pd s = pd.Series([1, 2, 3, 5, 8, 2, 4, 5, 3, 4, 5, 1]) print(s.value_counts()) d = pd.DataFrame([[1, 'a'], [2, 'a'], [3, 'c'], [5, 'c'], [2, 'a'], [2, 'd'], [4, 'd'], [5, 'c'], [3, 'c'], [4, 'd'], [5, 'c'], [1, 'a']]) print(d.iloc[:, 0].value_counts()) # 0列目(Series型)を取り出し、value_counts()を使用
# 実行結果 # s.values_counts() 5 3 4 2 3 2 2 2 1 2 8 1 dtype: int64 # d.iloc[:, 0].value_counts() 5 3 2 3 4 2 3 2 1 2 Name: 0, dtype: int64
上記の実行結果になります。Series中の要素の頻度が求められていることがわかります。
DataFrameには、value_counts()をそのまま使えません。使用したい場合は、apply()を使用します。apply()は、発展的な内容なので説明しませんが、例を載せておきます。
# sample_option.py import pandas as pd d = pd.DataFrame([[1, 'a'], [2, 'a'], [3, 'c'], [5, 'c'], [2, 'a'], [2, 'd'], [4, 'd'], [5, 'c'], [3, 'c'], [4, 'd'], [5, 'c'], [1, 'a']]) print(d.apply(pd.value_counts))
# 実行結果 0 1 5 3.0 NaN 2 3.0 NaN 4 2.0 NaN 3 2.0 NaN 1 2.0 NaN c NaN 5.0 a NaN 4.0 d NaN 3.0
要素の存在
要素の存在を求めるメソッドは、以下のものです。
- Series.isin(elements) : elements中の要素がSeriesの中に存在するかを求める。返り値は、真偽値の Series。
- DataFrame.isin(elements) : elements中の要素がDataFrameの中に存在するかを求める。返り値は、真偽値のDataFrame。
import pandas as pd s = pd.Series([1, 2, 3, 5, 8, 2, 4, 5, 3, 4, 5, 1]) print(s.isin([1, 3, 5])) d = pd.DataFrame([[1, 'a'], [2, 'a'], [3, 'c'], [5, 'c'], [2, 'a'], [2, 'd'], [4, 'd'], [5, 'c'], [3, 'c'], [4, 'd'], [5, 'c'], [1, 'a']]) print(d.isin([1, 3, 5, 'b', 'd'])) print(d.isin({0:[1, 3, 5], 1:['b', 'd']}))
# 実行結果 # s.isin([1, 3, 5]) 0 True 1 False 2 True 3 True 4 False 5 False 6 False 7 True 8 True 9 False 10 True 11 True dtype: bool # d.isin([1, 3, 5, 'b', 'd']) 0 1 0 True False 1 False False 2 True False 3 True False 4 False False 5 False True 6 False True 7 True False 8 True False 9 False True 10 True False 11 True # d.isin({0:[1, 3, 5], 1:['b', 'd']}) 0 1 0 True False 1 False False 2 True False 3 True False 4 False False 5 False True 6 False True 7 True False 8 True False 9 False True 10 True False 11 True False
上記の実行結果になります。DataFrameは、引数にlistとかdictを与えることができます。isin()関数は、フィルタリングするのによく使います。例を載せておきます。
# sample_option import pandas as pd s = pd.Series([1, 2, 3, 5, 8, 2, 4, 5, 3, 4, 5, 1]) s_ = s[s.isin([1, 3, 5])] # 要素が1,3,5だけを取り出す print(s_)
# 実行結果 0 1 2 3 3 5 7 5 8 3 10 5 11 1 dtype: int64
あとがき
お疲れさまでした。次回は、欠損値の処理について説明します。