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 – Pandas read_sql_query automatically rounds after 6 Digits

I’m trying to read data from sqlite3 database into a Dataframe. The problem is, that my numbers in the DB look like following 21.639929701230166. And if I print my df the number gets rounded to 21.639930. But I need the exact number for my PCA.

db_conn = sqlite3.connect(DATABASE_FILE)
sql_query = pd.read_sql_query('''SELECT AvgRT FROM sensordata''', db_conn)
df = pd.DataFrame(sql_query)

I saw that there is float_precision for csv. https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

Is there anything for SQL?

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

I already tried to cast it (like seen here: https://stackoverflow.com/a/70603287:

sql_query = pd.read_sql_query('''SELECT cast(AvgRT as decimal(25,25)) as AvgRT FROM sensordata''', db_conn)

but this results in the same number 21.639930 as before.

>Solution :

Yeah, the default precision in pandas is 6.

either you can use this pd.options.display.precision = 0 handy thing or use one of the below.

# (1) Round to specific decimal places – Single DataFrame column
df['DataFrame column'].round(decimals=number of decimal places needed)

# (2) Round up – Single DataFrame column
df['DataFrame column'].apply(np.ceil)

# (3) Round down – Single DataFrame column
df['DataFrame column'].apply(np.floor)

# (4) Round to specific decimals places – Entire DataFrame
df.round(decimals=number of decimal places needed)
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