gpt4 book ai didi

python - 将系列索引设置为另一个具有更多级别的索引

转载 作者:行者123 更新时间:2023-12-01 00:57:19 25 4
gpt4 key购买 nike

我正在尝试设置 Series (a1) 的索引,其索引 (ix) 包含的级别多于系列的原始索引。

>>> 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 :

For MultiIndex-ed objects to be indexed and sliced effectively, they need to be sorted. As with any index, you can use 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com