Home>

Bold text
I was able to understand how to create a csv file in the previous question, but I can't calculate using multiple cells, such as specifying multiple cells and finding the standard deviation.
I did a lot of research, but I didn't understand, so I asked a question.
Is there a way to find the standard deviation using only cells A1, A2, B1 and B2 in the source code below and enter the value in A6?
It may be faster to actually calculate, but I would appreciate it if you could tell me for the future.
■■ The following error message occurred while implementing the function.

Error message
Corresponding source code
import openpyxl as excel
import numpy as np
wb = excel.Workbook ()
ws = wb.active
ws ["A1"] = "26"
ws ["A2"] = "90"
ws ["A3"] = "32"
ws ["A4"] = "27"
ws ["B1"] = "20"
ws ["B2"] = "51"
ws ["B3"] = "26"
ws ["B4"] = "68"
ws ["C1"] = "73"
ws ["C2"] = "92"
ws ["C3"] = "35"
ws ["C4"] = "28"
ws ["D1"] = "30"
ws ["D2"] = "34"
ws ["D3"] = "15"
ws ["D4"] = "91"
What I tried

In addition to the above code
ws ["A6"] = np.std (["A1"], ["A2"], ["B3"], ["B4"])
I added, but I got a syntax error.

  • Answer # 1

    If you want to calculate with numbers, you should put numbers (26) in cells, not letters ("26").

    The data given to np.std () must be a list (or something like an array) with the first argument. In the current writing style, each value becomes an argument one by one.

    To see the value of a cell, you have to take the value property.

    So, after putting a number in the cell, I think that it will come out with the following formula.

    ws ["A6"] = np.std ([ws ["A1"]. value, ws ["A2"]. value, ws ["A3"]. value, ws ["A4"] .value])

    However, a word while being noisy
    I think that calculating the standard deviation means that we will also calculate the deviation value and average and put them in this cell.
    In that case, it is awkward to calculate after making an excel worksheet as a way of holding data.

    If I am myself, I will calculate it as a DataFrame of pandas, and when the data is ready, I will make it into an Excel workbook or spit it out with csv.
    If it is a DataFrame, various statistical calculations can be done easily.

  • Answer # 2

    You want to calculate the standard deviation using numpy instead of Excel and write it to Excel?

    There seem to be some misunderstandings.

    First,np.stdTakes a list.

    Sonp.std ([value 1, value 2, value 3, value 4])Should be like.

    Also, ["A1"] is just a list with the string A1, so what I wanted to do here wasws ["A1"]. valueit might be.

    In other words

    ws ["A6"] = np.std ([ws ["A1"]. value, ws ["A2"]. value, ws ["B3"]. value, ws ["B4"] .value])

    is.

    By the way

    ws ["A1"] = "26"

    Also

    ws ["A1"] = 26

    Is better.