多索引的Pandas Dataframe中2列之间的差异

时间:2018-08-16 18:34:54

标签: python pandas dataframe

我有一个Pandas DataFrame,其列为具有2个级别的MultiIndex,如下所示:

  <body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation"></button>
            @Html.ActionLink("Power", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            <div class="navbar-collapse collapse" id="navbarSupportedContent">

                <ul class="nav navbar-nav mr-auto">
                    <li class="nav-item">@Html.ActionLink("Home", "Index", "Home", new { @class = "nav-link" })</li>
                    <li class="nav-item">@Html.ActionLink("About", "About", "Home", new { @class = "nav-link" })</li>
                    <li class="nav-item">@Html.ActionLink("Contact", "Contact", "Home", new { @class = "nav-link" })</li>
                    <li class="nav-item dropdown show">
                        <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="true">Dropdown</a>
                        <div class="dropdown-menu" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 44px, 0px);">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#">Separated link</a>
                        </div>
                    </li>
                </ul>

            </div>
        </div>
    </nav>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Power Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>

我只是想将“旅行”和“食物”列中每个列的差异存储到新的顶级列中-例如'diff'-在“昨天”和“旅行”旁边

index = ['monday','tuesday','wednesday']
tuples = [('yesterday','travel'),('yesterday','food'),('today','travel'),('today','food')]
columns = pd.MultiIndex.from_tuples(tuples,names=[None,'category'])
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(3, 4)), index=index, columns=columns)

将返回我感兴趣的基础DataFrame,但我无法弄清楚如何将其正确放置在整个DataFrame中

类似:

diff = t['today'] - t['yesterday']

产生有趣(但不正确)的结果

1 个答案:

答案 0 :(得分:2)

一种方法可能是将diff的列设置为MultiIndex,例如:

diff = df['today'] - df['yesterday']
diff.columns = pd.MultiIndex.from_tuples([('diff',col) for col in diff.columns])

,然后当您使用concat时,它会给出:

print (pd.concat([df,diff],axis=1))
          yesterday       today        diff     
category     travel food travel food travel food
monday            8    7      7    1     -1   -6
tuesday           1    3      0    8     -1    5
wednesday         6    4      5    6     -1    2

编辑:不使用MultiIndex的另一种方法可能是直接执行创建列的操作:

df[[('diff','travel'),('diff','food')]] = df['today'] - df['yesterday']

还有一种更通用的方法,您可以使用get_level_values

df[[('diff',col) for col in df.columns.get_level_values(1).unique()]] = df['today'] - df['yesterday']