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
use pd.indexslice
df.loc[:, pd.indexslice[:, 'two']] *= 3 df
in case pd.indexslice[:, 'two']
specifying elements first level , 'two'
second level. using loc
allows assign df
.
Comments
Post a Comment