设置Pandas多重索引的值

16 投票
1 回答
26414 浏览
提问于 2025-04-18 02:58

我刚接触Python和Pandas这两个东西。

我想先创建一个数据框(dataframe),然后再给里面填充数据。

我已经创建好了我的数据框:

from pandas import *

ageMin = 21
ageMax = 31
ageStep = 2

bins_sumins = [0, 10000, 20000]
bins_age = list(range(ageMin, ageMax, ageStep))
indeks_sex = ['M', 'F']
indeks_age  =  ['[{0}-{1})'.format(bins_age[i-1], bins_age[i]) for i in range(1, len(bins_age))]
indeks_sumins = ['[{0}-{1})'.format(bins_sumins[i-1], bins_sumins[i]) for i in range(1, len(bins_sumins))]
indeks = MultiIndex.from_product([indeks_age, indeks_sex, indeks_sumins], names=['Age', 'Sex', 'Sumins'])

cols = ['A', 'B', 'C', 'D']

df = DataFrame(data = 0, index = indeks, columns = cols)

到目前为止一切都很好。我可以给一整组数据赋值:

>>> df['A']['[21-23)']['M'] = 1
>>> df
                           A  B  C  D
Age     Sex Sumins                   
[21-23) M   [0-10000)      1  0  0  0
            [10000-20000)  1  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[23-25) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[25-27) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[27-29) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0

但是,单独给某一个位置赋值就不行了……

>>> df['B']['[21-23)']['M']['[10000-20000)'] = 2
>>> df
                           A  B  C  D
Age     Sex Sumins                   
[21-23) M   [0-10000)      1  0  0  0
            [10000-20000)  1  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[23-25) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[25-27) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[27-29) M   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
        F   [0-10000)      0  0  0  0
            [10000-20000)  0  0  0  0
[16 rows x 4 columns]

这是怎么回事呢?我觉得我可能完全误解了多重索引(multiindexing)的用法。有人能帮我吗?

1 个回答

13

首先,看看关于链式索引的文档。

其次,阅读一下关于多重索引需要排序的内容。

这样你就能找到这个解决方案:

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将会有一些额外的方式来切片多重索引

撰写回答