Jessica Smith
Jessica Smith

Reputation: 63

Python Code for Standard Deviation with data from SQLITE3

from math import *
import sqlite3
ages=sqlite3.connect('person.sqlite3')

def main():
    ageslist=ages.execute("SELECT age from person")
#average age
    for row in ageslist:
        row[0]
    average = (sum(row[0]))/len(row[0])
#subtracts average x from x or opposite and square, depending on n    
    for n in range(len(ageslist) - 1):
        if numbers[n] > average:
            numbers.append((ageslist[n] - average)**2)
        if numbers[n] < average:
            numbers.append((average - ageslist[n])**2)
#takes square rt of the sum of all these numbers and divides by n-1
    Stdv = math.sqrt(sum(ageslist))/(len(ageslist)-1)
    end=time()
    print(Stdv)

main()

I am trying to find the standard deviation of the ages from an SQLITE3 db. However, I am getting the current error: average = (sum(row[0]))/len(row[0]) TypeError: 'int' object is not iterable

How can I correct this?

Upvotes: 1

Views: 908

Answers (2)

James
James

Reputation: 36691

The query sent to the database connection returns an iterator. You can only pass over that iterator once before it is flushed from memory. Here is some correction to your code to do what you are asking.

conn = sqlite3.connect('person.sqlite3')

def main():
    ages_iterator = conn.execute("SELECT age from person")
    # this turns the iterator into an actual list, which you need for stdev
    age_list = [a[0] for a in ages_iterator]

    # average age
    average = (sum(age_list))/len(age_list)

    # subtracts average x from x square
    # because you are squaring the difference, the it does not matter if it is
    # greater or less than the average
    numbers = [(age-average)**2 for age in age_list]

    #takes square rt of the sum of all these numbers and divides by n-1
    Stdv = math.sqrt(sum(numbers))/float(len(numbers)-1)
    end=time()
    print(Stdv)

main()

Upvotes: 1

Noah Christopher
Noah Christopher

Reputation: 166

Some quick comments in the code..

for row in ageslist:
    row[0]  # This statement does nothing
average = (sum(row[0]))/len(row[0]) # This statement will not have a row value to reference because your rows in ageslist will have been iterated through

When you execute ageslist=ages.execute("SELECT age from person") your ageslist variable is now an iterable object. Once you iterate through it you can no longer reference values in it without executing the database command again.

So I believe you should have a variable that sums the age during every row iteration in the for loop and also another variable that keeps a count of the number of entries in the database. This could be done in the for loop as well. Although I'm sure there is a more "pythonic" way to accomplish this.

Upvotes: 0

Related Questions