首页 > 解决方案 > Weird bug when changing boolean number to classify dataframe

问题描述

I have a dataframe where I am trying to add some boolean constraints that are numbers.

hrw_hotdry=combined_hrw[(combined_hrw['June_anom']<0) & (combined_hrw['June_anom_t'])>0]
hrw_hotdry.head()

Year    June_val    June_anom   July_val    July_anom   June_val_t  June_anom_t July_val_t  July_anom_t
0   1980    2.14    -1.40   0.99    -2.11   76.7    2.6 83.7    5.0
1   1981    2.85    -0.69   4.01    0.91    75.5    1.4 79.1    0.4
8   1988    2.08    -1.46   3.22    0.12    76.2    2.1 77.5    -1.2
10  1990    1.88    -1.66   3.16    0.06    77.3    3.2 76.7    -2.0
11  1991    3.13    -0.41   2.69    -0.41   75.1    1.0 78.4    -0.3

However, when I change the second constraint to 1 like this:

hrw_hotdry=combined_hrw[(combined_hrw['June_anom']<0) & (combined_hrw['June_anom_t'])>1]
hrw_hotdry.head()


Year    June_val    June_anom   July_val    July_anom   June_val_t  June_anom_t July_val_t  July_anom_t

There is no output. How does this make sense?

标签: pythonpandasdataframe

解决方案


括号不正确:

hrw_hotdry = combined_hrw[(combined_hrw['June_anom']<0) & (combined_hrw['June_anom_t']>1.0)]

    Year  June_val  June_anom  July_val  July_anom  June_val_t  June_anom_t  July_val_t  July_anom_t
0   1980      2.14      -1.40      0.99      -2.11        76.7          2.6        83.7          5.0
1   1981      2.85      -0.69      4.01       0.91        75.5          1.4        79.1          0.4
8   1988      2.08      -1.46      3.22       0.12        76.2          2.1        77.5         -1.2
10  1990      1.88      -1.66      3.16       0.06        77.3          3.2        76.7         -2.0

推荐阅读