Home > Back-end >  Python - Pandas read_sql_query automatically rounds after 6 Digits
Python - Pandas read_sql_query automatically rounds after 6 Digits

Time:07-05

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?

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.

CodePudding user response:

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)
  • Related