Pandas Concat 2组合每行



我为此而苦苦挣扎。

我有:

import pandas as pd
df1 = pd.Series([0, 1, 2, 3])
df2= pd.Series(["A", "B", "C", "D"])
df3 = pd.concat([df1, df2], axis=0)

我的结果是:

0
1
2
3 
A
B
C
D

我需要以下输出:

0
A
1
B 
2
C
3
D

reset_index添加sort_index,以避免重复的索引值:

df3 = pd.concat([df1, df2], axis=0).sort_index().reset_index(drop=True)
print (df3)
0    0
1    A
2    1
3    B
4    2
5    C
6    3
7    D
dtype: object

使用np.insert

   pd.Series(np.insert(df1.astype(str).values,np.arange(len(df1))+1,df2.values))
Out[1105]: 
0    0
1    A
2    1
3    B
4    2
5    C
6    3
7    D
dtype: object

最新更新