Home > database >  How to add sum() and mean() value above the df column values in the same line?
How to add sum() and mean() value above the df column values in the same line?

Time:12-08

Supposed we have a df with a sum() value in the below DataFrame, thanks so much for @jezrael 's answer here, now sum value is in the first line, and avg value is the second line, but it's ugly, how to let sum value and avg value in the same column and with index name:Total? Also place it in the first line as below

# Total                          27.56                 25.04                                         -1.31 

code in pandas is as below:

df.columns=['value_a','value_b','name','up_or_down','difference']

df1 = df[['value_a','value_b']].sum().to_frame().T
df2 = df[['difference']].mean().to_frame().T
df = pd.concat([df1,df2, df], ignore_index=True)
df

                                 value_a               value_b       name               up_or_down   difference
project_name
                                27.56               25.04  
                                                                                                     -1.31   
2021-project11                      0.43               0.48        2021-project11            up        0.05
2021-project1                        0.62               0.56          2021-project1            down       -0.06
2021-project2                       0.51               0.47       2021-project2           down       -0.04
2021-porject3                       0.37               0.34         2021-porject3            down       -0.03
2021-porject4                       0.64               0.61         2021-porject4            down       -0.03
2021-project5                        0.32               0.25          2021-project5            down       -0.07
2021-project6                       0.75               0.81         2021-project6            up        0.06
2021-project7                      0.60               0.60        2021-project7            down        0.00
2021-project8                       0.85               0.74         2021-project8            down       -0.11
2021-project10                      0.67               0.67        2021-project10            down        0.00
2021-project9                     0.73               0.73        2021-project9           down        0.00
2021-project11                   0.54               0.54      2021-project11           down        0.00
2021-project12                 0.40               0.40    2021-project12           down        0.00
2021-project13                      0.76               0.77        2021-project13            up        0.01
2021-project14                     1.16               1.28        2021-project14           up        0.12
2021-project15                     1.01               0.94        2021-project15           down       -0.07
2021-project16                      1.23               1.24        2021-project16            up        0.01
2022-project17                       0.40               0.36          2022-project17           down       -0.04
2022-project_11                      0.40               0.40        2022-project_11            down        0.00
2022-project4                        1.01               0.80          2022-project4            down       -0.21
2022-project1                        0.65               0.67          2022-project1            up        0.02
2022-project2                    0.75               0.57       2022-project2           down       -0.18
2022-porject3                       0.32               0.32         2022-porject3            down        0.00
2022-project18                       0.91               0.56         2022-project18            down       -0.35
2022-project5                        0.84               0.89          2022-project5            up        0.05
2022-project19                      0.61               0.48        2022-project19            down       -0.13
2022-project6                       0.77               0.80         2022-project6            up        0.03
2022-project20                      0.63               0.54         2022-project20           down       -0.09
2022-project8                       0.59               0.55         2022-project8            down       -0.04
2022-project21                       0.58               0.54         2022-project21            down       -0.04
2022-project10                      0.76               0.76        2022-project10            down        0.00
2022-project9                     0.70               0.71        2022-project9           up        0.01
2022-project22                    0.62               0.56      2022-project22            down       -0.06
2022-project23               2.03               1.74  2022-project23           down       -0.29
2022-project12                 0.39               0.39    2022-project12           down        0.00
2022-project24                      1.35               1.55         2022-project24           up        0.20
project25                         0.45               0.42           project25            down       -0.03
project26                         0.53                NaN         project26              down         NaN
project27                           0.68                NaN           project27              down         NaN

Thanks so much for any advice

CodePudding user response:

Use DataFrame.agg with dictionary for aggregate functions:

df.columns=['value_a','value_b','name','up_or_down','difference']

df1 = df.agg({'value_a':'sum', 'value_b':'sum', 'difference':'mean'}).to_frame('Total').T

df = pd.concat([df1,df])
print (df.head())
                value_a  value_b  difference            name up_or_down
Total             27.56    25.04   -0.035405             NaN        NaN
2021-project11     0.43     0.48    0.050000  2021-project11         up
2021-project1      0.62     0.56   -0.060000   2021-project1       down
2021-project2      0.51     0.47   -0.040000   2021-project2       down
2021-porject3      0.37     0.34   -0.030000   2021-porject3       down
  • Related