微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 设置值多索引熊猫

我是Python和Pandas的新手.

我正在尝试构建一个数据帧,然后用值填充它.

我构建了我的数据帧

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]

这里发生了什么?我对这个想法很开放,我完全误解了多索引的工作原理.任何人?

解决方法:

首先,看看chained indexing上的文档

其次,请阅读关于needing to sort MultiIndices内容.

这将使您获得此解决方案:

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将有一些additional ways for slicing a MultiIndex.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐