【问题标题】:DataFrame entries got round off when converted to txt转换为 txt 时,DataFrame 条目四舍五入
【发布时间】:2018-07-29 03:52:05
【问题描述】:

This is what the dataframe looks like before exporting

After that it becomes

四舍五入不是我想要的;我希望 txt.file 中的文本看起来像控制台中显示的那样。那么我该如何解决这个问题呢?有什么简单的解决方案吗?

【问题讨论】:

    标签: python-3.x pandas file dataframe rounding


    【解决方案1】:

    您是否尝试过直接从 Pandas 数据框编写而不是通过 Numpy?

    试试DF.to_csv(‘output.txt’, sep=‘\t’, float_format=‘%g’)

    更多详情见pandas.DataFrame.to_csv

    【讨论】:

    • 回答您的问题。现在我知道不止一种方法可以做到这一点。另外,我发现如果我在原始代码中也使用了 '%g',它会按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 2011-09-10
    • 2023-03-24
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    相关资源
    最近更新 更多