pandas 熊猫:具有不同列名的连接数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34282847/
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-14 00:23:03  来源:igfitidea点击:

pandas: concat data frame with different column name

pythonpandas

提问by arkisle

Suppose I have this data frame

假设我有这个数据框

id   x   y
0    a   hello
0    b   test
1    c   hi
2    a   hi
3    d   bar

I want to concat x and y into a single column like this keeping their ids

我想像这样将 x 和 y 连接到一个列中,并保留它们的 id

id   x
0    a
0    b
1    c
2    a
3    d
0    hello
0    test
1    hi
2    hi
3    bar

And how if I want to give a new name for the concat column? (e.g. 'x' to 'xy')

如果我想为 concat 列指定一个新名称怎么办?(例如“x”到“xy”)

采纳答案by Stefan

I don't think pandas.concatincludes the option to set new columnnames (see docs), but you could assign like so:

我认为不pandas.concat包括设置新column名称的选项(参见文档),但您可以像这样分配:

Starting from:

从...开始:

   id  x      y
0   0  a  hello
1   0  b   test
2   1  c     hi
3   2  a     hi
4   3  d    bar

df.set_index('id', inplace=True)
pd.DataFrame(pd.concat([df.x, df.y]), columns=['xy']).reset_index()

   id     xy
0   0      a
1   0      b
2   1      c
3   2      a
4   3      d
5   0  hello
6   0   test
7   1     hi
8   2     hi
9   3    bar

回答by jezrael

If ordering of rows is not important, you can use stack:

如果行的顺序不重要,您可以使用stack

print df
   id  x      y
0   0  a  hello
1   0  b   test
2   1  c     hi
3   2  a     hi
4   3  d    bar

s = df.set_index('id').stack()
s.index = s.index.droplevel(-1)
s.name = 'xy'

print pd.DataFrame(s).reset_index()
   id     xy
0   0      a
1   0  hello
2   0      b
3   0   test
4   1      c
5   1     hi
6   2      a
7   2     hi
8   3      d
9   3    bar