Python大 pandas :读取Excel文件时如何指定数据类型? [英] Python pandas: how to specify data types when reading an Excel file?

查看:821
本文介绍了Python大 pandas :读取Excel文件时如何指定数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pandas.read_excel()函数将一个excel文件导入大熊猫数据框。

I am importing an excel file into a pandas dataframe with the pandas.read_excel() function.

其中一列是表的主键:它是所有数字,但它作为文本存储(Excel单元格左上角的小绿色三角形证实了这一点)。

One of the columns is the primary key of the table: it's all numbers, but it's stored as text (the little green triangle in the top left of the Excel cells confirms this).

但是,当我将文件导入熊猫数据框时,列将作为浮点数导入。这意味着,例如,'0614'成为614.

However, when I import the file into a pandas dataframe, the column gets imported as a float. This means that, for example, '0614' becomes 614.

导入列时是否有方法指定数据类型?我理解这是可能导入CSV文件,但找不到任何东西在 read_excel()的语法。

Is there a way to specify the datatype when importing a column? I understand this is possible when importing CSV files but couldn't find anything in the syntax of read_excel().

我可以想到的唯一的解决方案是在文本开头添加一个随意的字母(将0614转换成A0614),以确保在Excel中列作为文本导入,然后在python中截取A,因此我可以将其与从SQL导入的其他表匹配。

The only solution I can think of is to add an arbitrary letter at the beginning of the text (converting '0614' into 'A0614') in Excel, to make sure the column is imported as text, and then chopping off the 'A' in python, so I can match it to other tables I am importing from SQL.

推荐答案

你只是指定转换器。我创建了以下结构的excel电子表格:

You just specify converters. I created an excel spreadsheet of the following structure:

names   ages
bob     05
tom     4
suzy    3

年龄列的格式为字符串。要加载:

Where the "ages" column is formatted as strings. To load:

import pandas as pd

df = pd.read_excel('Book1.xlsx',sheetname='Sheet1',header=0,converters={'names':str,'ages':str})
>>> df
       names ages
   0   bob   05
   1   tom   4
   2   suzy  3

这篇关于Python大 pandas :读取Excel文件时如何指定数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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