设定值多索引 pandas [英] Set value multiindex Pandas
本文介绍了设定值多索引 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python和Pandas的新手.
I'm a newbie to both Python and Pandas.
我正在尝试构造一个数据框,然后再用值填充它.
I am trying to construct a dataframe, and then later populate it with values.
我已经构建了数据框
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)
到目前为止,一切都很好.我可以将值分配给整个值
So far all is well. I am able to assign value to a whole set of values
>>> 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
但是,仅设置一个位置的值是不可行的...
however, setting the value of one position only is a no go...
>>> 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]
这是怎么回事?我对我完全误解了多索引的工作方式持开放态度.有人吗?
What is going on here? I am open to the idea that i have completely misunderstood how multiindexing works. Anyone?
推荐答案
首先,看看第二,阅读有关这将使您获得此解决方案:
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
将有一些其他切片MultiIndex的方法.
pandas .14
will have some additional ways for slicing a MultiIndex.
这篇关于设定值多索引 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文