numpyを使った複数要素の一括比較演算

[`evernote` not found]
Bookmark this on Hatena Bookmark
Share on Facebook
LINEで送る

まとめて複数要素の比較をし,andを取る

import numpy as np
a=np.array([1,2,3])
b=np.array([1,2,3])
c=np.array([1,2,4])
print a==b,' is ',(a==b).all()
print b==c,' is ',(b==c).all()

出力

[ True  True  True]  is  True
[ True  True False]  is  False

まとめて複数要素の比較をし,その配列でDataFrameをフィルタ

import numpy as np
import pandas
df = pandas.read_csv(open(...))
under50 = df[df['axis1']<50]
between50_100 = df[np.logical_and(df['axis1']>=50,df['axis1']<100)]