【问题标题】:Converting .csv file into scientific notation with pandas使用 pandas 将 .csv 文件转换为科学计数法
【发布时间】:2021-10-21 11:29:24
【问题描述】:

我正在尝试使用 pandas 转换 csv 文件。

def convert_file(file, columns, name):
    df = pd.read_csv(file, header=0, delimiter=',')
    df.drop(df.columns[columns], axis=1, inplace=True)
    df.to_csv(name, index=False, header=False)

删除不需要的列后,我想将数字转换为科学格式。 我有什么选择?

例子:

当前结果 -> 期望结果

0.0053455117 -> 5.3455117e-003

0.88455491 -> 8.8455491e-001

10.576477 -> 1.0576477e+001

【问题讨论】:

标签: python pandas csv scientific-notation


【解决方案1】:

我有什么选择?

利用float_format.to_csv。考虑以下示例

import pandas as pd
df = pd.DataFrame({'x':[0.1,0.01,0.001]})
df.to_csv("file.csv",float_format="%e",header=False,index=False)

产生file.csv

1.000000e-01
1.000000e-02
1.000000e-03

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 2011-11-20
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多