python - Round columns based on second level of MultiColumn -


i have table looks this:

>>> df.head() out[13]:                                  v                   u                                                    init    change  integral      init    change         foo          bar                                                      baseline          nan    0.025054  0.858122  0.017930  0.048435  1.091943                    10.0   0.025042  0.856307  0.017546  0.047815  1.100351                      50.0   0.025008  0.856681  0.010052  0.048252  1.056658           b          1.0    0.025045  0.858044  0.015635  0.047135  1.091384                      2.0    0.025048  0.855388  0.016115  0.047324  1.087964  

now select columns based on label of second level of column, , round them.

i can access them using xs: df.xs('init', 1, 1). however, naturally cannot use xs replace values:

>>> df.xs('init', 1, 1) = df.xs('init', 1, 1).round(decimals=3)   file "<ipython-input-12-47c16e5011a3>", line 1     df.xs('init', 1, 1) = df.xs('init', 1, 1).round(decimals=3) syntaxerror: can't assign function call 

what's way go here?

consider dataframe:

df = pd.dataframe(np.arange(8).reshape(2, 4),                   ['a', 'b'],                   pd.multiindex.from_product([['a', 'b'], ['one', 'two']]))  df 

enter image description here

use pd.indexslice

df.loc[:, pd.indexslice[:, 'two']] *= 3  df 

enter image description here

in case pd.indexslice[:, 'two'] specifying elements first level , 'two' second level. using loc allows assign df.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -