Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing and overwriting Multiindex df data

I’m trying to multiply all the values of the following multiindex df for which the first multiindex equals Property_2 with a scalar:

(index_1,index_2)    Col1
Property_1     A      1
               B      2
               C      3
Property_2     D      1
               E      2
               F      3

I’ve tried various ways:

df.loc[Property_2,:] = df.loc[Property_2,:]*0.01
df.loc[(Property_2,:),:] = df.loc[(Property_2,:),:]*0.01

but I am getting back nan’s in the relevant places.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s because the indices don’t match. One way to get around the issue is to assign the underlying numpy array:

df.loc['Property_2', 'Col1'] = df.loc['Property_2', 'Col1'].mul(0.01).to_numpy()

or you could use mask:

df['Col1'] = df['Col1'].mask(df.index.get_level_values(0) == 'Property_2', df['Col1']*0.01)

Output:

                    Col1
index_1    index_2      
Property_1 A        1.00
           B        2.00
           C        3.00
Property_2 D        0.01
           E        0.02
           F        0.03
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading