2

by reading excel with pandas trying to convert floats to string with all decimal points


df = pd.read_excel('data/DHTest.xlsx',  keep_default_na=False, na_filter=False, dtype=str)

input in excel -0.00001 results to output in parquet -1e-05

--

if I try to run without dtype=str


df = pd.read_excel('data/DHTest.xlsx',  keep_default_na=False, na_filter=False, dtype=str)

results from -0.00001 to -0.00001 but by converting to string df = df.astype('string') creates again -1e-05

I need to write parquet with all columns as string df.to_parquet('data/DHTest.parquet') with output -0.00001 and not -1e-05

1
  • 1
    df['column'].map('{:,.6f}'.format) Commented Nov 12, 2020 at 12:58

1 Answer 1

2
import pandas as pd
#Dummy Data
l = [0.0000001,0.00000002]
df = pd.DataFrame(l, columns=["data"])
-->
           data
0  1.000000e-07
1  2.000000e-08

#Transform (fixed number of zeros)
df["data"] = df["data"].map('{:,.8f}'.format)
-->
          data
0  0.00000010
1  0.00000002

#Transform (max number of zeros, but remove trailing)
df["data"] = df["data"].apply(lambda x: ('{:,.8f}'.format(x)).rstrip("0"))
-->
         data
0   0.0000001
1  0.00000002
Sign up to request clarification or add additional context in comments.

2 Comments

I have input 0.1 to 0.00001. df["data"].map('{:,.8f}'.format) will create 0.10000000 and 0.0000100. Is there a way to keep output without additional 0?
I included a version, that removes trailing zeros

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.