簡體   English   中英

Pandas DataFrame)一列替換其他df

[英]Pandas DataFrame) one column replace other df

我有兩個 pandas DataFrame

# python 3
one is | A | B | C | and another is | D | E | F |
       |---|---|---|                |---|---|---|
       | 1 | 2 | 3 |                | 3 | 4 | 6 |
       | 4 | 5 | 6 |                | 8 | 7 | 9 |
       | ......... |                | ......... |

我想得到“預期”的結果

expected result : 

       | A | D | E | F | C |               
       |---|---|---|---|---|               
       | 1 | 3 | 4 | 6 | 3 |               
       | 4 | 8 | 7 | 9 | 6 |               
       | ................. |
        
df1['B'] convert into df2

       

我努力了

pd.concat([df1,df2], axis=1, sort=False)

and drop column df['B']

但它似乎效率不高。 可以通過使用 insert() 或其他方法解決嗎?

我認為您的方法很好,您也可以在concat之前刪除列:

pd.concat([df1.drop('B', axis=1),df2], axis=1, sort=False)

DataFrame.join的另一種方法:

df1.drop('B', axis=1).join(df2)

暫無
暫無

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

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