Search code examples
pythonpandasmean

How to save mean and standard deviation (STD) in pandas csv file


Let's make the file Sheet1 data the same as the contents of sheet2

I want to find the average and standard deviation (std) of dozens of columns from one column.

I need help

example

sheet1:

BK 1    BK 2    BK 3    BK 4    BK 5    BK 6    BK 7    BK 8
63.06   55.57   82.67   75.95   78.76   63.89   63.49   295.42
64.18   55.97   83.13   75.7    77.32   64.28   63.99   324.03
64.34   56.12   84.32   77.84   78.06   65.29   64.5    331.83
64.52   56.37   81.93   74.98   76.43   62.7    62.4    312.32
65.56   56.55   81.7    75.07   76.8    61.81   61.24   316.27
62.87   55.38   79.51   73      76.39   60.96   60.94   279.24
63.37   55.37   81.51   75.26   78.28   62.64   61.76   303.52
64.31   55.97   82.17   76.01   78.43   62.69   61.88   311.37
64.95   56.88   79.88   73.66   74.93   61.32   60.39   273.44
65.22   56.76   79.34   74.75   76.69   60.7    59.09   303.75
65.41   56.84   81.57   75.76   76.74   62.5    62.12   311.73
67.16   58.62   81.39   75.79   78.19   61.61   60.95   277.03
65.84   57.28   80.25   77.03   78.31   61.48   60.44   334.4
66.56   57.92   81.89   77.45   80.03   61.87   60.97   338.56

sheet2:

     BK 1   BK 2    BK 3    BK 4    BK 5    BK 6    BK 7    BK 8
mean 64.81  56.54   81.52   75.59   77.53   62.41   61.73   308.07
STD  1.26   0.94    1.40    1.33    1.29    1.32    1.49    21.02
     63.06  55.57   82.67   75.95   78.76   63.89   63.49   295.42
     64.18  55.97   83.13   75.7    77.32   64.28   63.99   324.03
     64.34  56.12   84.32   77.84   78.06   65.29   64.5    331.83
     64.52  56.37   81.93   74.98   76.43   62.7    62.4    312.32
     65.56  56.55   81.7    75.07   76.8    61.81   61.24   316.27
     62.87  55.38   79.51   73      76.39   60.96   60.94   279.24
     63.37  55.37   81.51   75.26   78.28   62.64   61.76   303.52
     64.31  55.97   82.17   76.01   78.43   62.69   61.88   311.37
     64.95  56.88   79.88   73.66   74.93   61.32   60.39   273.44
     65.22  56.76   79.34   74.75   76.69   60.7    59.09   303.75
     65.41  56.84   81.57   75.76   76.74   62.5    62.12   311.73
     67.16  58.62   81.39   75.79   78.19   61.61   60.95   277.03
     65.84  57.28   80.25   77.03   78.31   61.48   60.44   334.4
     66.56  57.92   81.89   77.45   80.03   61.87   60.97   338.56

help me

I searched the internet but couldn't find what I was looking for.


Solution

  • You can use concat with agg/round :

    import pandas as pd
    import numpy as np
    
    df = pd.read_csv("input.csv") # or pd.read_excel(input.xlsx)
    
    out = pd.concat([df.agg(["mean", "std"]).round(5), df])
    out.index = ["mean", "std"] + [np.nan]*(len(out)-2)
    
    out.to_csv("output.csv", sep=";") #or out.to_excel("output.xlsx")
    

    Output :

    enter image description here