如何使用Pandas to_string编写具有不同精度的csv文件?

时间:2019-01-29 22:12:35

标签: python python-3.x pandas

我有一个数据框,格式为:

3  -1305.55 -1.638500e+01 -689.773  4.020000e-02
13  2301.26  6.385300e+11 -189.170  1.040270e+06

我无法使用以下格式:

df.to_string(fn, formatters=[" %0.2f ", " %0.5e ", " %0.2f ", " %0.5e"])

我知道了

TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:2)

正如评论者所述,formatters必须是可调用对象的列表或字典。使用Python的新格式很容易做到这一点。

print(df.to_string(
    formatters=['{:0.2f}'.format, '{:0.5e}'.format, '{:0.2f}'.format, '{:0.5e}'.format]))
          0            1       2           3
3  -1305.55 -1.63850e+01 -689.77 4.02000e-02
13  2301.26  6.38530e+11 -189.17 1.04027e+06