添加索引列并通过匹配部分索引标签重新索引数据帧

时间:2018-03-29 08:58:26

标签: python pandas multi-index reindex

我有一个多指数df s:

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
pd.MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],
       labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],
       names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)
s

我想添加一个新的索引列"零"通过匹配索引列"第一个"使用x,y,z到s和"第二"。换句话说,我想重复三次,但是这个额外的索引列有x,y,z。我尝试了reindex(见下文),但为什么它给了我所有的NaN?

mux=pd.MultiIndex.from_product([["x","y","z"], 
                            s.index.get_level_values(0),
                            s.index.get_level_values(1)],
                           names=["zero","first", "second"])
t=s.reindex(mux)
t

我也尝试将匹配级别指定为" first"和"秒",但看起来级别只需要一个整数?

2 个答案:

答案 0 :(得分:2)

您可以使用reindex,但必须通过MultiIndex创建levels。但它会为现有内容添加新级别,因此如有必要,请添加reorder_levelssort_index

np.random.seed(123)
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

s = pd.Series(np.random.randn(8), index=index)
#print (s)
mux=pd.MultiIndex.from_product([s.index.levels[0],s.index.levels[1], ["x","y","z"]])
t=s.reindex(mux, method='ffill').reorder_levels([2,0,1]).sort_index()
print (t)
x  bar  one   -1.085631
        two    0.997345
   baz  one    0.282978
        two   -1.506295
   foo  one   -0.578600
        two    1.651437
   qux  one   -2.426679
        two   -0.428913
y  bar  one   -1.085631
        two    0.997345
   baz  one    0.282978
        two   -1.506295
   foo  one   -0.578600
        two    1.651437
   qux  one   -2.426679
        two   -0.428913
z  bar  one   -1.085631
        two    0.997345
   baz  one    0.282978
        two   -1.506295
   foo  one   -0.578600
        two    1.651437
   qux  one   -2.426679
        two   -0.428913
dtype: float64

答案 1 :(得分:1)

IIUC,您想要pd.concat吗?

s = pd.concat([s] * 3, axis=0, keys=['x', 'y', 'z'])

如果需要,重命名轴:

s = s.rename_axis(['zero', 'first', 'second'])

s 

zero  first  second
x     bar    one       0.510567
             two       0.066620
      baz    one       0.667948
             two      -1.471894
      foo    one       1.881198
             two       0.143628
      qux    one       1.108174
             two      -0.978112
y     bar    one       0.510567
             two       0.066620
      baz    one       0.667948
             two      -1.471894
      foo    one       1.881198
             two       0.143628
      qux    one       1.108174
             two      -0.978112
z     bar    one       0.510567
             two       0.066620
      baz    one       0.667948
             two      -1.471894
      foo    one       1.881198
             two       0.143628
      qux    one       1.108174
             two      -0.978112
dtype: float64