我有一个像这样的数据框:
df1:
col_1, col_2
a,1
b,2
c,3
d,4
我想用2列的交换位置来连接df1两次,这样我就可以了:
col_a, col_b
a,1
b,2
c,3
d,4
1,a
2,b
3,c
4,d
我尝试过:
df2 = pd.DataFrame(columns=['col_a','col_b'])
df2[['col_a','col_b']] = pd.concat([df1[['col_1','col_2]],df1[['col_2','col_1']]])
但是,这只是将数据框与自身连接在一起,因此结果是:
col_a, col_b
a,1
b,2
c,3
d,4
a,1
b,2
c,3
d,4
正确的方法是什么?
答案 0 :(得分:2)
使用//does it calculate the height of the left and the right together or what
concat
答案 1 :(得分:0)
从numpy
pd.DataFrame(np.vstack([df.values,df.values[:,::-1]]),columns=df.columns)
Out[850]:
col_1 col_2
0 a 1
1 b 2
2 c 3
3 d 4
4 1 a
5 2 b
6 3 c
7 4 d