Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Boolean indexing of multi-index Dataframes

Say I have the following:

                   X         Y  
A    B                          
bar  one    0.094315 -0.816244  
     three -1.316294 -0.383182  
flux six   -0.176711  0.117511  
     three -1.401581  1.471682  
foo  five   0.647646 -0.422405  
     one   -0.319071  1.598857  
     two   -0.234279  0.082658  
     two    0.423970  1.418249  

and that I get some boolean Series indexing my Dataframe, e.g. my_series = df['X'] > 0.

I would like use this my_series to fill-in specific entries on column Y. I could certainly do:

df[my_series]['Y'] = 1

but this would write on a view. How can I combine boolean-based indexing with regular label indexing?

Upvotes: 0

Views: 102

Answers (1)

Phillip Cloud
Phillip Cloud

Reputation: 25652

Use df.loc[df.X > 0, 'Y'] = 1.

Upvotes: 4

Related Questions