来自数据帧序列的多索引数据帧 [英] Multi-index dataframe from sequence of dataframes

查看:96
本文介绍了来自数据帧序列的多索引数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个数据框[df1, df2, df3]的列表,其中每个单个数据框的外观如下:

Say I have a list of dataframes [df1, df2, df3], where each single dataframe looks as follows:

> df1 

            median   std
control        0.4   0.2
experiment     0.2   0.3

如何创建将它们缝合在一起的 multi-index 数据框?像这样:

How can I create a multi-index dataframe that stitches them together? Like this:

                         df1                 df2                  df3
          control experiment  control experiment  control  experiment
median        0.4        0.2      ...       ...      ...          ...
std           0.2        0.3      ...       ...      ...          ...

推荐答案

因此,您可以将数据帧作为字典提供(如重复的问题:

So you can provide the dataframes as a dict (as in duplicate question: python/pandas: how to combine two dataframes into one with hierarchical column index?), and then the dict keys are used:

pd.concat({'df1':df1, 'df2':df2, 'df3':df3}, axis=1)

或另一种选择是使用keys关键字参数:

or another option is to use the keys keyword argument:

pd.concat([df1, df2, df3], axis=1, keys=['df1', 'df2', 'df3'])

这篇关于来自数据帧序列的多索引数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆