如何将两个 Pandas Dataframe 列堆叠在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27513890/
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
How do you stack two Pandas Dataframe columns on top of each other?
提问by aus_lacy
Is there a library function or correct way of stacking two Pandas data frame columns on top of each other?
是否有库函数或将两个 Pandas 数据框列堆叠在一起的正确方法?
For example make 4 columns into 2:
例如将 4 列变成 2 列:
a1 b1 a2 b2
1 2 3 4
5 6 7 8
to
到
c d
1 2
5 6
3 4
7 8
The documentation for Pandas Data Frames that I read for the most part only deal with concatenating rows and doing row manipulation, but I'm sure there has to be a way to do what I described and I am sure it's very simple.
我阅读的大部分 Pandas Data Frames 文档只处理连接行和进行行操作,但我确信必须有一种方法来执行我所描述的操作,而且我确信它非常简单。
Any help would be great.
任何帮助都会很棒。
回答by Carsten
You can select the first two and second two columns using pandas.DataFrame.iloc. Then, change the column name of both parts to cand d. Afterwards, you can just join them using pandas.concat.
您可以使用 选择前两列和后两列pandas.DataFrame.iloc。然后,将两个部分的列名称更改为c和d。之后,您可以使用 加入他们pandas.concat。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(1, 9).reshape((2, 4)),
columns=["a1", "b1", "a2", "b2"])
part1 = df.iloc[:,0:2]
part2 = df.iloc[:,2:4]
new_columns = ["c", "d"]
part1.columns = new_columns
part2.columns = new_columns
print pd.concat([part1, part2], ignore_index=True)
This gives you:
这给你:
c d
0 1 2
1 5 6
2 3 4
3 7 8
回答by evanv
I would do the following
我会做以下
import pandas as pd
df = pd.DataFrame({'a1' : pd.Series([1,5]), 'b1' : pd.Series([2,6]), 'a2' : pd.Series([3,7]), 'b2' : pd.Series([4,8])})
df1 = df[['a1','b1']]
df2 = df[['a2','b2']]
df1.columns = ['c','d']
df2.columns = ['c','d']
df1.append(df2)
I just saw that @Carsten answered this question as well and I agree with his answer too
我刚看到@Carsten 也回答了这个问题,我也同意他的回答
回答by dgmp88
Alternatively, using melt:
或者,使用melt:
# Make data as in previous answers
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(1, 9).reshape((2, 4)),
columns=["a1", "b1", "a2", "b2"])
# Melt both columns and concatenate
df = pd.concat([
df[['a1', 'a2']].melt(value_name='c'),
df[['b1', 'b2']].melt(value_name='d')],
axis=1)
# Discard unwanted columns melt creates
df = df[['c', 'd']]

