[英]Dataframe convert float to string with all decimal
by reading excel with pandas trying to convert floats to string with all decimal points通过读取 excel 和 pandas 尝试将浮点数转换为所有小数点的字符串
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输入 excel -0.00001 结果到 output in parquet -1e-05
-- --
if I try to run without dtype=str如果我尝试在没有 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结果从 -0.00001 到 -0.00001 但通过转换为字符串
df = df.astype('string')
再次创建 -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我需要将所有列的镶木地板写成字符串
df.to_parquet('data/DHTest.parquet')
with output -0.00001 而不是 -1e-05
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.