首页 > 解决方案 > 从列表创建数据框时防止熊猫舍入整数

问题描述

df1 = pd.DataFrame(listoflists, columns =['size', 'text'])

我将 a 转换listoflists为两列 pandasdf1

listoflists = [[10.5, 'qqqqq \t\n'], [11.0, 'rrrreee \n'], [10.5, 'rrrrr \n'], [10.000000000000057, 'ertttt \n'], [7.500000000000007, 'ert: ert \n']]

colsize会自动舍入。我怎样才能防止熊猫这样做?我想保留所有小数。

print(df1)

              size                                               text
0             10.5                                         qqqqq \t\n
1             11.0                                         rrrreee \n
2             10.5                                           rrrrr \n
3             10.0                                          ertttt \n
4             7.5                                         ert: ert \n

我试过

df1.style.format({
    'size': '{:,.3f}'.format})

但这并没有帮助

标签: python-3.xpandasdataframerounding

解决方案


数据存储正确。为了以您喜欢的精度查看它,请在导入 pandas 后使用此命令:

pd.set_option("display.precision", 15)

将精度位数设置为您需要的数量。


推荐阅读