read_csv导入大量数字 [英] read_csv import large numbers

查看:118
本文介绍了read_csv导入大量数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例CSV文件的内容类似于

A sample CSV file has contents like

Afghanistan,AFG,8013233121.55065,8689883606.07776,8781610175.40574

当我使用

GDP = pd.read_csv('world_bank.csv', header=4, usecols=fields) 

我用科学计数法得到数字.

I get the numbers in scientific notation.

Afghanistan,AFG,3.992331e+12,4.559041e+12

要使用的正确转换器是什么?

What is the correct converter to use?

推荐答案

这些数字是浮点数.您只是在查看显示的内容.

Those numbers are floats. You are just seeing what is displayed.

请考虑以下内容

txt = """Afghanistan,AFG,8013233121.55065,8689883606.07776,8781610175.40574"""

GDP = pd.read_csv(StringIO(txt), header=None, converters={i:np.float128 for i in [2,3,4]})

print(GDP)

             0    1             2             3             4
0  Afghanistan  AFG  8.013233e+09  8.689884e+09  8.781610e+09


但是,仔细观察一个单元格


However, a closer look at one cell

GDP.iloc[0, 2]

8013233121.5506496


对于print,您可以使用浮点格式. pd.set_option('display.float_format', '{:0.6f}'.format)或临时执行


To print with a float format you like you can. pd.set_option('display.float_format', '{:0.6f}'.format) or do it temporarily

with pd.option_context('display.float_format', '{:0.6f}'.format):
    print(GDP)

             0    1                 2                 3                 4
0  Afghanistan  AFG 8013233121.550650 8689883606.077761 8781610175.405741

这篇关于read_csv导入大量数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆