简体   繁体   English

导出到 excel 文件时,如何抑制 Pandas DF 中的科学记数法?

[英]How do I suppress scientific notation in pandas DF when exporting to an excel file?

I need to export a Data Frame to an excel file with big natural numbers without decimals like 1234567890123456789 by example, and the output in the excel file should be a number (not a string).我需要将一个数据框导出到一个带有大自然数的 excel 文件中,例如 1234567890123456789,并且excel文件中的输出应该是一个数字(而不是字符串)。

I have found a lot of solutions to suppress scientific notation, but those solutions are using a string instead of a number, or you need to use a number with decimals like 0.00001 by example (and I need to use just natural numbers, not decimals).我找到了很多抑制科学记数法的解决方案,但是这些解决方案使用的是字符串而不是数字,或者您需要使用带有小数的数字,例如 0.00001(我只需要使用自然数,而不是小数) .

There is any way to do what I need?有什么办法可以做我需要的吗?

Just in case, here is how I manage the data:以防万一,这是我管理数据的方式:

# Reading:
excel_file = pd.ExcelFile(filename_in)
# I read some data frames like this:
df = pd.read_excel(excel_file, sheet)
# I perform operations with the data frames

# Exporting to excel (.xlsx):
excel_writer = pd.ExcelWriter(filename_out, engine='xlsxwriter')

# I do this with all the data frames:
df.to_excel(excel_writer, sheet_name=sheet, index=False)

# After all the changes:
excel_writer.save()

Integers like 1234567890123456789 are too big to be handled by Excel.像 1234567890123456789 这样的整数太大,Excel 无法处理。

Numbers in Excel are stored as IEEE 754 Doubles which have a (general) precision of 15 digits. Excel 中的数字存储为IEEE 754 双精度数,其(一般)精度为 15 位。 So, if you pasted 1234567890123456789 into Excel it would be stored in the file format as 1.2345678901234501E+18 and displayed, depending on the format, as 1234567890123450000.因此,如果您将 1234567890123456789 粘贴到 Excel 中,它将以文件格式存储为 1.2345678901234501E+18 并根据格式显示为 1234567890123450000。

So, in short, you aren't going to be able to store numbers like that in Excel without losing precision or without storing them as strings (hence the recommendations you saw elsewhere).因此,简而言之,您将无法在不丢失精度或不将它们存储为字符串的情况下在 Excel 中存储这样的数字(因此是您在其他地方看到的建议)。

For the more general question here is an example of setting the number format for a dataframe output to Excel:对于更一般的问题,这里是将数据帧输出的数字格式设置为 Excel 的示例:

import pandas as pd

# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Numbers':    [0.001112, 0.002224, 0.003335, 0.004547]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("pandas_column_formats.xlsx", engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Add a number format.
format1 = workbook.add_format({'num_format': '0.00000'})

# Set the column width and format.
worksheet.set_column(1, 1, 18, format1)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我从 Pandas 的 excel 中读取十六进制字符串时,如何抑制 python 中的科学记数法? - How do I suppress scientific notation in python when I read hexadecimal string from excel by pandas? 熊猫:抑制科学记数法? - pandas: suppress scientific notation? 如何抑制 pandas dataframe 中值的科学记数法? - How to suppress scientific notation in values in a pandas dataframe? pandas to_csv:在将熊猫写入 csv 时抑制 csv 文件中的科学记数法 - pandas to_csv: suppress scientific notation in csv file when writing pandas to csv 如何抑制 Pandas 中的科学计数法并在折线图末尾添加 label? - How to suppress the scientific notation in Pandas and add the label at the end of the line chart? 禁止 Pandas 中的科学记数法 *不*改变精度 - Suppress scientific notation in Pandas *without* altering precision 如何在控制 Python 中的有效数字的同时抑制科学记数法? - How do I suppress scientific notation while controlling significant figures in Python? 使用 Pandas 抑制整列行的科学计数法 - Suppress scientific notation for a whole column line with Pandas 在 pandas 中的 to_markdown() 中禁止科学记数法 - Suppress scientific notation in to_markdown() in pandas 打印浮点值时如何抑制科学记数法? - How to suppress scientific notation when printing float values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM