Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Calculating Standard Deviation using SQLLite db data with statistics library

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: can’t convert type ‘tuple’ to numerator/denominator

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading