1

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

1 Answer 1

2

You could:

selection = df[df.weight!=0].drop('tb_91day_tri', level=1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.