简体   繁体   中英

Pandas dataframe remove rows based on index and column value

I have a multi index data frame, called df, and I want to remove all rows with symbol (2nd level of multi index) = 'tb_91day_tri' and weight (column) = 0. I know it is easy to remove rows by index and rows by column value, but I cannot figure out how to combine both.

                                             weight
asofdate   symbol                                  
2015-11-02 universe_tri                      0.000000
           tb_91day_tri                      0.053984
           tri_us78463x749                   0.051751
           tri_ca46431a109                   0.018860
           tri_ca46430l106                   0.067672
           tri_ca46430j101                   0.197639
           tri_ca46428l100                   0.151681
           tri_ca46428j105                   0.108409
           tse_300_bl_tri                    0.000000
           tb_91day_tri                      0.000000
           sp_500_tri                        0.000000


df.drop(df.xs('tb_91day_tri',level=1)[df.xs('tb_91day_tri',level=1)['weight']==0]) #doesn't work

你可以:

selection = df[df.weight!=0].drop('tb_91day_tri', level=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM