簡體   English   中英

熊貓連接每行2個數據幀

[英]Pandas concat 2 dataframes combining each row

我為此感到困惑。

我有:

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

添加sort_indexreset_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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM