【问题标题】:Pandas Multi index DataFrame add subindex to each indexPandas Multi index DataFrame 为每个索引添加子索引
【发布时间】:2021-05-05 02:10:35
【问题描述】:

我有一个多索引数据框,其中包含“bar”和“baz”行,并且每一行都有“one”和“two”行。我现在想在每行“bar”和 foo”中添加一行“三”。

有没有优雅的方法来做到这一点?

例如:

import pandas as pd
import numpy as np

arrays = [["bar", "bar", "baz", "baz"],
          ["one", "two", "one", "two"]]

tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
df = pd.DataFrame(np.random.randn(3, 4), index=["A", "B", "C"], columns=index)
In [38]: df
Out[38]: 
first        bar                 baz          
second       one       two       one       two
A       0.357392 -1.880279  0.099014  1.354570
B       0.474572  0.442074 -1.173530 -1.362059
C      -0.980140 -0.173440 -1.490654 -0.539123

我想要这样的东西:

first        bar                           baz                    
second       one       two     three       one       two     three
A      -0.096890  0.012150       nan -0.749569 -0.965033       nan
B      -0.854206  0.118473       nan  0.263058 -0.025849       nan
C      -0.688007 -0.258569       nan  0.127305 -0.955044       nan

【问题讨论】:

    标签: python pandas dataframe multi-index


    【解决方案1】:

    对于general answer,当您不一定知道 0 级索引的名称并且通常希望对每个 0 级索引执行此操作时:

    首先,我们应该创建要注入的NaN 矩阵。它有len(df) 行数,对于列,我们应该找到数据框中有多少0 级列。创建它之后,我们将其设为具有与我们的多索引数据帧相同的索引和列的数据帧。请注意,对于这个数据帧,我们只需要原始数据帧的levels[0],因为对于下一个级别,我们希望拥有'three'

    a = np.full((len(df),len(df.columns.levels[0])), np.nan)
    
    inject_df = pd.DataFrame(a, index=df.index, columns=pd.MultiIndex.from_product([df.columns.levels[0], ['three']]))
    inject_df
    
    first  bar     baz
           three   three
    A      NaN     NaN
    B      NaN     NaN
    C      NaN     NaN
    

    最后,我们将注入的 df 与原始的 df 连接起来,并对索引进行排序,使共享 level(0) 索引的那些放在一起。

    result = pd.concat([df, inject_df], axis=1).sort_index(level=0, axis=1)
    result
    
    first   bar                         baz
    second  one    three    two         one        three    two
    A    -0.995944  NaN   -0.437629    -0.629472    NaN    1.919711
    B    -0.402886  NaN   0.262420      0.117202    NaN    -1.234542
    C    1.281046   NaN   -1.058977     0.447767    NaN    2.374122
    
    

    【讨论】:

      【解决方案2】:

      我不知道它有多像 Python,但有两种方法可以做到这一点:简单替换和使用插入。

      1. 替换
      df[('bar','three')] = np.NaN
      df[('baz','three')] = np.NaN 
      
      1. 插入
      df.insert(2,('bar','three'),np.NaN)
      df.insert(5,('baz','three'),np.NaN)
      first                   bar                     baz
      second  one     two     three   one     two     three
      A   -0.973338   -0.233507   NaN     0.777288    -2.282688   NaN
      B   -0.377486   0.080627    NaN     0.401302    0.355696    NaN
      C   0.481056    0.651335    NaN     0.161145    1.001937    NaN
      
      

      【讨论】:

        猜你喜欢
        • 2015-12-13
        • 2018-01-18
        • 2018-06-14
        • 2018-12-23
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        相关资源
        最近更新 更多