簡體   English   中英

Pandas 數據框連接和排序索引

[英]Pandas data frame Concatenate and Sorting the Index

我有一個帶有一個分類列的數據框,我將數據框划分為根據類別進行操作,現在想要合並數據集,

index A   B   C   
 1    M   2.2 3.4 
 2    F   3.4 1.0
 3    M   3.0 2.1
  .
  .
  .

創建了兩個新的數據框

index A   B   C   
 1    M   2.2 3.4 
 3    M   3.0 2.1
      .
      .
      .



   index A   B   C   
     2   F   3.4 1.0
          .
          .
          .

我想將結果輸出為:

index A   B   C    D
 1    M   2.2 3.4  2.8
 2    F   3.4 1.0  3.4
 3    M   3.0 2.1  2.05
      .
      .
      .

我嘗試進行合並和連接,但沒有獲得所需的解決方案。

您可以創建一個數據框列表,然后連接所有

import pandas as pd
frames = [df1, df2] # df1 and df2 are two dataframes
result = pd.concat(frames)

或者你可以簡單地使用 append

result = df1.append(df2)

更好的方法是:

result = pd.concat([df1, df4], ignore_index=True, sort=False)

來源: https : //pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

暫無
暫無

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

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