pandas 熊猫读取excel:不解析数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24508686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas read excel: do not parse numbers
提问by Felix
I am working with python pandas and MS excel to edit a xlsx file. I iterate between these programs back and forth. The file contains some columns with text that looks like numbers, e.g.,
我正在使用 python pandas 和 MS excel 来编辑 xlsx 文件。我在这些程序之间来回迭代。该文件包含一些带有看起来像数字的文本的列,例如,


If I read this, I get
如果我读到这个,我得到
pd.read_excel ('test.xlsx')
A
0 1
1 100
and
和
pd.read_excel ('test.xlsx').dtypes
A int64
dtype: object
My question is: how is it possible to read the text as text? It is not an option to parse it back after reading, because part of the information (i.e., the leading zeros) is lost upon conversion to a number.
我的问题是:如何将文本作为文本阅读?读取后解析它不是一种选择,因为部分信息(即前导零)在转换为数字时丢失。
Thank you for your help.
感谢您的帮助。
采纳答案by RJT
According to this issue, it's a known problem with pandas.
根据这个问题,这是Pandas的一个已知问题。
回答by D Read
You can work around the known issue(assuming that you know the column name) by using the 'converters' parameter:
您可以使用 'converters' 参数解决已知问题(假设您知道列名):
>>> pd.read_excel('test.xlsx', converters={'A': str})
A
0 001
1 100
>>> pd.read_excel('test.xlsx', converters={'A': str}).dtypes
A object
dtype: object
回答by Spryger
if you're able to convert the file to CSV, dtype=str should work.
如果您能够将文件转换为 CSV,则 dtype=str 应该可以工作。
pd.read_csv('test.csv', dtype=str)
Source: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html
来源:https: //pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

