我正在尝试使用包含比系列原始索引更多级别的索引(Series)设置a1ix)的索引。

>>> a = pd.DataFrame({'a': [1, 2, 3], 'b': ['b', 'b', 'b'], 'x': [4, 5, 6]}).set_index(['a', 'b'])
>>> a
     x
a b
1 b  4
2 b  5
3 b  6
>>>
>>> a1 = a['x']
>>> a1
a  b
1  b    4
2  b    5
3  b    6
Name: x, dtype: int64

>>> ix = pd.MultiIndex.from_product(([1, 2, 3], ['b', 'c'], [10, 20]), names=['a', 'b', 'c'])
>>> ix
MultiIndex(levels=[[1, 2, 3], [u'b', u'c'], [10, 20]],
           labels=[[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]],
           names=[u'a', u'b', u'c'])
>>> a.set_index(ix)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python2.7/site-packages/pandas/core/frame.py", line 3164, in set_index
    frame.index = index
  File "python2.7/site-packages/pandas/core/generic.py", line 3627, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "python2.7/site-packages/pandas/core/generic.py", line 559, in _set_axis
    self._data.set_axis(axis, labels)
  File "python2.7/site-packages/pandas/core/internals.py", line 3074, in set_axis
    (old_len, new_len))
ValueError: Length mismatch: Expected axis has 3 elements, new values have 12 elements


因此,我期望以下系列:

a  b   c
1  b  10  4
2  b  10  5
3  b  10  6
1  c  10  nan   # [1, c] wasn't an index in a1
2  c  10  nan   # ...
3  c  10  nan   # ...
1  b  20  4     # [1, b] was an index of a1, so use that value
2  b  20  5     # ...
3  b  20  6     # ...
1  c  20  nan   # [1, c] wasn't an index in a1
2  c  20  nan   # ...
3  c  20  nan   # ...

# if there was an index in a1 that isn't in `ix`, it should be maintained with
# its value and the index should be augmented


如何使用Pandas实现这一目标?

最佳答案

使用Series.reindex,但是输出有些不同,因为对MultiIndex进行了排序,因此,如果以后要有效地工作,显然有必要-source


  为了有效地索引MultiIndex -ed对象并对其进行切片,需要对它们进行排序。与任何索引一样,您可以使用sort_index()。


a = a.reindex(ix)
print (a)
          x
a b c
1 b 10  4.0
    20  4.0
  c 10  NaN
    20  NaN
2 b 10  5.0
    20  5.0
  c 10  NaN
    20  NaN
3 b 10  6.0
    20  6.0
  c 10  NaN
    20  NaN

关于python - 将系列索引设置为具有更多级别的另一个索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56142781/

10-11 23:22
查看更多