Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas Dataframe filter not working

I have a Pandas dataframe called df with the following content:

    Symbol                     Cat         Beta Eps MktCap
2350    FBP  Foreign Regional Banks            0   0      0
2351   FNBC  Foreign Regional Banks            0   0      0
2353   BSBR  Foreign Regional Banks            0   0      0
2354    BBD  Foreign Regional Banks            0   0      0
2355    HDB  Foreign Regional Banks            0   0      0
2356    BCH  Foreign Regional Banks            0   0      0
2358     WF  Foreign Regional Banks            0   0   None
2359   SMFG  Foreign Regional Banks            0   0   None
2360    BFR  Foreign Regional Banks            0   0      0
2361    BCA  Foreign Regional Banks            0   0      0
2362   BPOP  Foreign Regional Banks            0   0   None
2363    CIB  Foreign Regional Banks            0   0      0
2364   ITUB  Foreign Regional Banks            0   0      0
2365    BMA  Foreign Regional Banks            0   0      0
2366     KB  Foreign Regional Banks            0   0   None
2367   BBDO  Foreign Regional Banks            0   0      0
2368   BSMX  Foreign Regional Banks            0   0   None
2369   BBVA  Foreign Regional Banks            0   0   None
2370    SHG  Foreign Regional Banks            0   0      0
2352     DB  Foreign Regional Banks         1.08   0      0
2357    MFG  Foreign Regional Banks  6.101694915   0   None

I use the following Python code:

df2 = df[df.Beta > 0]

The resulting df2 does not filter out the 0 values for Beta meaning it stays equal to df. How do I fix this? Thanks

like image 589
Bryan Downing Avatar asked Oct 18 '22 21:10

Bryan Downing


1 Answers

I think you can try cast to float by astype - it seems dtype of column Beta is object (then type is obviously string):

df2 = df[df.Beta.astype(float) > 0]
like image 102
jezrael Avatar answered Oct 20 '22 10:10

jezrael