Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 从2行中交织行_Pandas_Concat - Fatal编程技术网

Pandas 从2行中交织行

Pandas 从2行中交织行,pandas,concat,Pandas,Concat,我有两个df,我想将这两个df中的行交织起来,以构建一个df import pandas as pd df1 = pd.DataFrame({'SONY': [1,3,5,7,9],}, index=['a','b','c','d','e']) df1 = pd.DataFrame({'SONY': [2,4,6,8,10],}, index=['a','b','c','d','e']) 预期产量 SONY a 1 a 2 b 3 b 4 c 5 c 6 d

我有两个df,我想将这两个df中的行交织起来,以构建一个df

import pandas as pd
df1 = pd.DataFrame({'SONY': [1,3,5,7,9],}, index=['a','b','c','d','e'])
df1 = pd.DataFrame({'SONY': [2,4,6,8,10],}, index=['a','b','c','d','e'])
预期产量

   SONY
a   1
a   2
b   3
b   4
c   5
c   6
d   7
d   8
e   9
e   10
我的尝试失败了,我想在dfs上循环,提取行并将它们放入列表中,然后从中构建df

dfs=[]
numofrows = len(df.index)
for x in range(0, numofrows):
    dfs.append(pd.concat([df1.iloc[x], df2.iloc[x]], ignore_index=True))

为什么我要这样做:我试图在图表后面复制df,这就是它的样子。

您应该使其索引不同,然后使用
zip
,并且:

[外]


不抱歉,我应该在索引中使用不同的项
import numpy as np

# Change indices
df1.index+='1'
df2.index+='2'

order = np.hstack(list(zip(df1.index, df2.index)))
df = pd.concat([df1, df2]).reindex(order)
print(df)
    SONY
a1     1
a2     2
b1     3
b2     4
c1     5
c2     6
d1     7
d2     8
e1     9
e2    10