Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 重新编制一个系列的索引将返回大熊猫中的南_Python 3.x_Pandas_Series_Multi Index_Reindex - Fatal编程技术网

Python 3.x 重新编制一个系列的索引将返回大熊猫中的南

Python 3.x 重新编制一个系列的索引将返回大熊猫中的南,python-3.x,pandas,series,multi-index,reindex,Python 3.x,Pandas,Series,Multi Index,Reindex,以下代码返回一个到处都是NAN的序列: s = pd.Series([1, 2, 3, 4, 5, 6], index=pd.MultiIndex.from_product([["A", "B"], ["c", "d", "e"]])) s.reindex([('E', 'g'), ('E', 'h'), ('E', 'i'), ('F', 'g'), ('F', 'h'), ('F', 'i')]) 或 如何重新索引序列并保留原始值?这不是重新索引,即更改

以下代码返回一个到处都是NAN的序列:

s = pd.Series([1, 2, 3, 4, 5, 6],
                 index=pd.MultiIndex.from_product([["A", "B"], ["c", "d", "e"]]))

s.reindex([('E', 'g'), ('E', 'h'), ('E', 'i'), ('F', 'g'), ('F', 'h'), ('F', 'i')])


如何重新索引序列并保留原始值?

这不是
重新索引
,即更改
索引

s.index=pd.MultiIndex.from_product([['E', 'F'], ['g', 'h', 'i']])
s
Out[362]: 
E  g    1
   h    2
   i    3
F  g    4
   h    5
   i    6
dtype: int64

如果需要将新值设置为二级使用:

是的,“reindex”的意思是“查找这些值”,而不是“替换索引”
s.index=pd.MultiIndex.from_product([['E', 'F'], ['g', 'h', 'i']])
s
Out[362]: 
E  g    1
   h    2
   i    3
F  g    4
   h    5
   i    6
dtype: int64
s.index = s.index.set_levels(['g', 'h', 'i'], level=1)
print (s)
A  g    1
   h    2
   i    3
B  g    4
   h    5
   i    6
dtype: int64