1、目的
獲取高溫小於30,低溫大於20的行數據
2、分析
df['最高溫度']<30返回的是一列Series對象,內容為bool類型,此時原DataFrame df[bool類型]就會獲取高溫小於30的數據
3、實現
df1 = df[(df['最低溫度']>20) & (df['最高溫度']<30)]
4、錯誤案例
4.1 pandas中不能使用and作為與運算符
df1 = df[(df['最低溫度']>20) and (df['最高溫度']<30)]
報錯:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
4.2 運算順序
&的優先級高於<,不加括號會導致報錯
df1 = df[df['最低溫度']>20 & df['最高溫度']<30]
報錯:
TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]