如何在pandas csv文件中保存均值和标准差(STD)

hyrbngr7  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(118)

让我们使文件Sheet1的数据与sheet2的内容相同
我想从一列中找出几十列的平均值和标准差(std)。
我需要帮助
范例
表1:

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

表2:

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

帮帮我
我在互联网上搜索,但找不到我要找的东西。

8aqjt8rx

8aqjt8rx1#

您可以将concatagg/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")

输出:

nhn9ugyo

nhn9ugyo2#

你可以试试这样

import pandas as pd

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

sheet2 = pd.concat([sheet1.describe().loc[['mean', 'std']], sheet1],
               ignore_index=False).round(2)
print(sheet2)

表2:

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.06
std    1.26   0.94   1.40   1.33   1.29   1.32   1.49   21.02
0     63.06  55.57  82.67  75.95  78.76  63.89  63.49  295.42
1     64.18  55.97  83.13  75.70  77.32  64.28  63.99  324.03
2     64.34  56.12  84.32  77.84  78.06  65.29  64.50  331.83
3     64.52  56.37  81.93  74.98  76.43  62.70  62.40  312.32
4     65.56  56.55  81.70  75.07  76.80  61.81  61.24  316.27
5     62.87  55.38  79.51  73.00  76.39  60.96  60.94  279.24
6     63.37  55.37  81.51  75.26  78.28  62.64  61.76  303.52
7     64.31  55.97  82.17  76.01  78.43  62.69  61.88  311.37
8     64.95  56.88  79.88  73.66  74.93  61.32  60.39  273.44
9     65.22  56.76  79.34  74.75  76.69  60.70  59.09  303.75
10    65.41  56.84  81.57  75.76  76.74  62.50  62.12  311.73
11    67.16  58.62  81.39  75.79  78.19  61.61  60.95  277.03
12    65.84  57.28  80.25  77.03  78.31  61.48  60.44  334.40
13    66.56  57.92  81.89  77.45  80.03  61.87  60.97  338.56

希望它能回答你的问题。

相关问题