This may be a two-part question, but I'm looking for the best way to rescale (or perform any operation) in a subset of the records identified by their secondary index.
For example, let's say I have the following data frame:
>>> df=pd.DataFrame(data=[[1,2,3],[.4,.5,.6],[7,8,9],[.10,.11,.12]], index=pd.MultiIndex.from_tuples([(1,'a'), (1,'b'), (2,'a'), (2,'b')]), columns=['Var1','Var2','Var3'])
>>> df.index.names=['Number','Letter']
>>> print df
Var1 Var2 Var3
Number Letter
1 a 1.0 2.00 3.00
b 0.4 0.50 0.60
2 a 7.0 8.00 9.00
b 0.1 0.11 0.12
I want the two entries indicated by the letter โbโ to have all 3 of the variables multiplied by 10.
The first aspect I'm struggling with is how to choose the second multi-index. I can do this with the following messy workaround, but I would suggest that there is a cleaner way:
>>> df=df.reset_index().set_index(['Letter','Number'])
>>> Records=df.loc['b']
>>> print Records
Var1 Var2 Var3
Number
1 0.4 0.50 0.60
2 0.1 0.11 0.12
Any suggestions on a better way to subset the second index?
And then I can rescale them:
>>> print Records*10
Var1 Var2 Var3
Number
1 4 5 6
2 10 11 12
However, how do I replace the original values โโwith these newly changed values?