Skip to content Skip to sidebar Skip to footer

Set Value Multiindex Pandas

I'm a newbie to both Python and Pandas. I am trying to construct a dataframe, and then later populate it with values. I have constructed my dataframe from pandas import * ageMin

Solution 1:

First off, have a look at the docs on chained indexing

Second, read this about needing to sort MultiIndices.

That will get you to this solution:

In [46]: df = df.sort_index()

In [47]: df.loc['[21-23)', 'M', '[10000-20000)'] = 2

In [48]: df
Out[48]: 
                           A  B  C  D
Age     Sex Sumins                   
[21-23) F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        M   [0-10000)      0  0  0  0
            [10000-20000)  2  2  2  2
[23-25) F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[25-27) F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[27-29) F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0

[16 rows x 4 columns]

pandas .14 will have some additional ways for slicing a MultiIndex.

Post a Comment for "Set Value Multiindex Pandas"