在Pandas CSV阅读器中指定数据类型 [英] Specifying data type in Pandas csv reader

查看:62
本文介绍了在Pandas CSV阅读器中指定数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Pandas,并且正在使用 read_csv() 方法.我遇到的困难是阻止熊猫将我的电话号码转换为大数字,而不是将它们保留为字符串.我定义了一个转换器,该转换器只保留数字,但后来仍然转换为数字.当我更改转换器以在电话号码前加一个"z"时,它们就停留在字符串上.有什么方法可以保持它们的字符串而不修改字段的值吗?

I am just getting started with Pandas and I am reading in a csv file using the read_csv() method. The difficulty I am having is preventing pandas from converting my telephone numbers to large numbers, instead of keeping them as strings. I defined a converter which just left the numbers alone, but then they still converted to numbers. When I changed my converter to prepend a 'z' to the phone numbers, then they stayed strings. Is there some way to keep them strings without modifying the values of the fields?

推荐答案

从Pandas 0.11.0开始,您可以使用dtype参数为每个列显式指定数据类型:

Since Pandas 0.11.0 you can use dtype argument to explicitly specify data type for each column:

d = pandas.read_csv('foo.csv', dtype={'BAR': 'S10'})

这篇关于在Pandas CSV阅读器中指定数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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