将多索引添加到 Pandas 数据帧并保持当前索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20085308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:21:46  来源:igfitidea点击:

Add multi-index to pandas dataframe and keep current index

pythonpandasdataframemulti-index

提问by TheChymera

I am trying to merge time-course data from different participants. I am iteratively extracting a dataframe per participant and concatenating them at the end of the loop. Before I concatenate, I would like to add the ID of my participants to an additional index.

我正在尝试合并来自不同参与者的时间课程数据。我迭代地为每个参与者提取一个数据帧,并在循环结束时将它们连接起来。在连接之前,我想将参与者的 ID 添加到附加索引中。

This seems REALLY straightforward, but I was unable to find anything on this issue :(

这看起来真的很简单,但我在这个问题上找不到任何东西:(

I would like to turn this

我想转这个

    col
0     1
1   1.1
2   NaN

Into:

进入:

          col
ID    0     1
      1   1.1
      2   NaN

I know I could make a new index like:

我知道我可以创建一个新索引,例如:

multindex = [np.array(ID*len(data)),np.array(np.arange(len(data)))]

But that's inelegant without end, and - seeing as I am measuring with high frequency over half an hour - would even get kind of slow :/

但那是无止境的不优雅,而且 - 看到我在半小时内高频测量 - 甚至会变得有点慢:/

I would like to mention that I have recently found my question to be a duplicate of this other question. However mine apparently has more upvotes and better answers. “Prepend” apparently doesn't seem to draw as many hits.

我想提一下,我最近发现我的问题与另一个问题重复。然而,我的显然有更多的赞成和更好的答案。“Prepend”显然没有吸引那么多点击。

回答by HYRY

Maybe you can use keysargument of concat:

也许您可以使用以下keys参数concat

import numpy as np
import pandas as pd

df1 = pd.DataFrame(np.random.rand(3, 2))
df2 = pd.DataFrame(np.random.rand(4, 2))
df3 = pd.DataFrame(np.random.rand(5, 2))

print pd.concat([df1, df2, df3], keys=["A", "B", "C"])

output:

输出:

            0         1
A 0  0.863774  0.794880
  1  0.578503  0.418619
  2  0.215317  0.146167
B 0  0.655829  0.116917
  1  0.862316  0.812847
  2  0.500126  0.689218
  3  0.653439  0.270427
C 0  0.825213  0.882963
  1  0.579436  0.332047
  2  0.456948  0.718893
  3  0.795074  0.826773
  4  0.049676  0.697471

If you want to append other dataframes later:

如果您想稍后附加其他数据帧:

df4 = pd.DataFrame(np.random.rand(6, 2))
pd.concat([df, pd.concat([df4], keys=["D"])])