python-2.7 - 将科学计数法转换为十进制 pandas python

标签 python-2.7 pandas

可能这是一个老问题,我在下面发现了类似的问题,但我仍然可以在输出文件中看到科学记数法。

Suppressing scientific notation in pandas?

Pandas read scientific notation and change

Python Pandas Scientific Notation Iconsistent

我尝试将 set_optiondf.apply(pd.to_numeric, args=('coerce',)) 等合并到我的代码中,但不起作用.

df = pd.read_csv(Input)  

dfNew = df[['co_A','co_B','co_C']]  
# I firstly select columns from df then would like to convert scientific notation to decimal type in my output file.

dfNew.to_csv(Output, index = False, sep = '\t')

我仍然可以在输出文件中看到科学记数法。谁能帮忙?

co_A  co_B  co_C
167 0.0 59.6
168 0.0 60.6
191 8e-09   72.6
197 -4.7718e-06 12.3
197 0.0 92.4
198 0.0 39.5

最佳答案

调用.to_csv()时可以使用float_format参数功能:

In [207]: df
Out[207]:
   co_A          co_B  co_C
0   167  0.000000e+00  59.6
1   168  0.000000e+00  60.6
2   191  8.000000e-09  72.6
3   197 -4.771800e-06  12.3
4   197  0.000000e+00  92.4
5   198  0.000000e+00  39.5

In [208]: fn = r'D:\temp\.data\out.csv'

In [209]: df.to_csv(fn, index=False, sep='\t', float_format='%.6f')

输出.csv:

co_A    co_B    co_C
167 0.000000    59.600000
168 0.000000    60.600000
191 0.000000    72.600000
197 -0.000005   12.300000
197 0.000000    92.400000
198 0.000000    39.500000

关于python-2.7 - 将科学计数法转换为十进制 pandas python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810328/

相关文章:

python - 根据其他字段重新计算pandas数据框字段的更好方法

python - Django:表单验证接受首字母大写的字符串

python - Pandas:在半重叠的列上连接两个数据框

pandas - 从 Pandas 中按月分组的数据创建多索引

python - Matplotlib 标签被剥离/重叠

Python 数据将行格式化为列

python - 如何将多列中的特定值替换为另一个数据帧的相应值?

python - 如何缩短长 "and"语句?

python - 在 Python 中测试抽象类

Python:如何使用浏览器碎片检查复选框?