簡體   English   中英

如何在熊貓中並排附加數據框中的兩列

[英]how to append two columns in a dataframe side by side in pandas

我有一個名為df_final的數據框輸入,如下所示:

 location      2022-01   2022-02    2022-03   2022-01   2022-02  2022-03  
      AGH      2062.25   6561.43   3154.43     0        0        0
      AIM      3122.74   3277.56   3631.45     0        0        0
      AJL      1641.82   3229.49   4937.23     0        0        0
 .................

第一批月份(前三列月份)實際上是總成本,而后三列實際上是總CN。 我生成此輸出的代碼如下所示:

# Get monthly delivery CN
    total_delivery_CN = monthly_delivery_CN_by_branch(start_date, end_date)
    
  # Get monthly pickup CN
    total_pickup_CN = monthly_pickup_CN_by_branch(start_date, end_date)

  # Get monthly maintainence cost:
    total_maintainence_cost = monthly_maintainence_cost_by_branch(start_date, end_date)

  # Get monthly refuel cost:
    total_refuel_cost = monthly_refuel_cost_by_branch(start_date, end_date)
    
  # Union 
    df = pd.concat([total_delivery_CN, total_pickup_CN, total_maintainence_cost, total_refuel_cost])
    print('Union')
    print(df)
    df.to_csv('df.csv')
    
  # Pivot
    df_final = df.pivot_table(index=['location'], columns = ['report_month'], aggfunc = {'total_CN' : np.sum, 'Cost' : np.sum} , fill_value=0)
    
    df_final.columns = df_final.columns.to_flat_index().to_series().apply(lambda x: x[1])
    df_final = df_final.reset_index()
    df_final.to_csv('df_final.csv')
    
    print(df_final)

但是,我會在同一個月合並兩個總數,因此最終輸出如下所示:

                    2022-01                  2022-02                  2022-03       
     location     Cost     total_CN      Cost     total_CN       Cost     total_CN
      AGH        2062.25     0            6561.43     0          3154.43      0                 
      AIM        3122.74     0            3277.56     0          3631.45      0               
      AJL        1641.82     0            3229.49     0          4937.23      0               
 .................

我如何在同一個月下追加這兩個總數?

我從數據框開始:

df = pd.DataFrame(
    [
        [2,3,4,0.2,0.3,0.4],
        [2,3,4,0.2,0.3,0.4],
        [2,3,4,0.2,0.3,0.4]
    ], 
    index=('AGH', 'AIM', 'AJL'),
    columns=('2022-01', '2022-02', '2022-03', '2022-01', '2022-02', '2022-03')
).rename_axis('location')

看起來像

          2022-01  2022-02  2022-03  2022-01  2022-02  2022-03
location                                                      
AGH             2        3        4      0.2      0.3      0.4
AIM             2        3        4      0.2      0.3      0.4
AJL             2        3        4      0.2      0.3      0.4

這就是你要找的:

def flatten(l):
    return [item for sublist in l for item in sublist]

def create_multiindex(cols):
    return pd.MultiIndex.from_tuples(
        flatten([[(char, 'Cost'), (char, 'total_CN')] for char in sorted(set(cols))])
    )

df = df[sorted(set(df.columns))]
df.columns = create_multiindex(df.columns)

它產生:

         2022-01          2022-02          2022-03         
            Cost total_CN    Cost total_CN    Cost total_CN
location                                                   
AGH            2      0.2       3      0.3       4      0.4
AIM            2      0.2       3      0.3       4      0.4
AJL            2      0.2       3      0.3       4      0.4

您可以將 df_final 分割成兩個數據幀,然后您可以像我們在此處所做的那樣將兩個幀連接起來:

df_cost = df_final.iloc[:, 0:3]
df_total_cn = df_final.iloc[:, 3:6]
df = pd.concat([df_cost, df_total_cn], axis=1, keys=['cost', 'total cn']).swaplevel(axis=1).sort_index('columns').fillna(0)
print(df)


             2022-01           2022-02           2022-03
             cost total cn     cost total cn     cost total cn
location
AGH       2062.25        0  6561.43        0  3154.43        0
AIM       3122.74        0  3277.56        0  3631.45        0
AJL       1641.82        0  3229.49        0  4937.23        0

暫無
暫無

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

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