Skip to content
Advertisement

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.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement