LoginSignup
86
49

More than 5 years have passed since last update.

【Python】pandas.DataFrameで複数条件指定時のエラーの対処

Posted at

pandas.DataFrameで複数条件を指定したときによく出るエラーの対処方法。

準備
import numpy as np
import pandas as pd

cols = ['var1', 'var2', 'var3', 'var4']
df = pd.DataFrame(np.random.randn(4, 4), columns=cols)
df

       var1      var2      var3      var4
0  0.597118 -0.853204  1.813645  0.694750
1 -1.118426  0.011119 -2.161933  0.792262
2  0.665828  0.384975  1.676278 -0.487037
3 -0.216118  2.084042 -0.279242 -1.785128
エラーその1
df[df['var1'] >= 0 and df['var2'] <= 0.5]

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
エラーその2
df[(df['var1'] >= 0) and (df['var2'] <= 0.5)]

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
エラーその3
df[df['var1'] >= 0 & df['var2'] <= 0.5]

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
上手くいくパターン
df[(df['var1'] >= 0) & (df['var2'] <= 0.5)]

       var1      var2      var3      var4
0  0.597118 -0.853204  1.813645  0.694750
2  0.665828  0.384975  1.676278 -0.487037

要するに,「条件ごとに括弧で括る」+「&で接続」ということですね。

86
49
2

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
86
49