Pandas选项将前导零输入项视为字符串 [英] Pandas option to treat leading zeros entry as string

查看:94
本文介绍了Pandas选项将前导零输入项视为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 read_csv 选项可以将以零开头的列解析为字符串?我已经看到有关预处理和修改csv以添加引号的建议,但是我想知道是否 52个kwargs选项可以为我完成此操作。

Is there a read_csv option to parse columns with leading zeros as strings? I've seen suggestions to preprocess and modify the csv to add quotes, but am wondering if one of the 52 kwargs options can do this for me.

类似于这个问题,但明确询问是否存在熊猫选项,而不是预处理或手动传递 dtype = {strcols:object}

Similar to this question, but explicitly asking if there's a pandas option, rather than preprocessing or manually passing dtype={strcols:object}.

推荐答案

询问,您将收到。将dtype指定为{列名称:类型}的字典:

Ask, and you shall receive. Specify the dtype as a dictionary of {column name: type} :

In [22]: s = """0001,2
0002,3
"""

In [23]: pd.read_csv(StringIO(s), header=None, dtype={0: str})
Out[23]: 
      0  1
0  0001  2
1  0002  3

[2 rows x 2 columns]

这篇关于Pandas选项将前导零输入项视为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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