I have a pandas dataframe with two columns, col 1 with text in it and col 2 with decimal values.
| Key | Value |
|---|---|
| A | 1.2089 |
| B | 5.6718 |
| B | 7.3084 |
I use the '.apply' function to set the data type of the value column to Decimal (Python Decimal library). Once I do this the Value column goes from a 4 decimal place value to 43 decimal places. I have attempted to use the .getcontect.prec = 4 to no avail.
The data frame is constructed from reading a CSV file with the same format as the table above. All the decimal numbers in the value column are only given to 4 decimal places.
import pandas as pd
from decimal import *
def get_df(table_filepath):
df = pd.read_csv(table_filepath)
getcontect.prec = 4
df['Value'] = df['Value'].apply(Decimal)
The above code is what I have tried but still results in a output with the value column values having 43 decimal places rather than the 4 decimal places each value should have as read from the csv file.
The result I get when I print the dataframe is:
| Key | Value |
|---|---|
| A | 1.20890000000003046807250939309597015380859375 |
| B | 5.67180000000000318323145620524883270263671875 |
| B | 7.30838399999999969077180139720439910888671875 |
I only want 4 decimals of precision because these values will be use to do some maths later on and I want to work with the exact values I provided.