Home > other >  Python Calculating Standard Deviation using SQLLite db data with statistics library
Python Calculating Standard Deviation using SQLLite db data with statistics library

Time:12-24

I am able to get data of the column I want but I cant use its data, because it is in tuple form, to calculate its standard deviation. I get following errors.

cursor.execute("select SMA from PRICES")
smaColum = cursor.fetchall()
std = statistics.stdev(smaColum)

return (x.numerator, x.denominator)

AttributeError: 'tuple' object has no attribute 'numerator'

TypeError: can't convert type 'tuple' to numerator/denominator

CodePudding user response:

Once you have converted the tuple to a list, you should be able to perform the mathematical operation that was causing the "TypeError: can't convert type 'tuple' to numerator/denominator" error.

Here is an example of Python code that calculates the standard deviation of a column of data in an SQLite database table using the statistics library:

import sqlite3
import statistics

# Connect to the database
conn = sqlite3.connect("database.db")

# Create a cursor
cursor = conn.cursor()

# Execute a query to select the data from the desired column
cursor.execute("SELECT column FROM table")

# Store the data in a list
data = [row[0] for row in cursor.fetchall()]

# Calculate the standard deviation of the data using the stdev() function from the statistics library
standard_deviation = statistics.stdev(data)

# Print the result
print(standard_deviation)

# Close the connection to the database
conn.close()

Example to Converting from tuple to list

# Convert a tuple to a list
tuple = (1, 2, 3)
list = [x for x in tuple]

print(list)  # Output: [1, 2, 3]
  • Related