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

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

问题描述

我正在使用 pandas.read_excel() 函数将一个 excel 文件导入到 Pandas 数据框中.

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).

但是,当我将文件导入到 Pandas 数据框中时,该列将作为浮点数导入.这意味着,例如,'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().

我能想到的唯一解决方案是在Excel中的文本开头添加任意字母(将'0614'转换为'A0614'),以确保该列作为文本导入,然后将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天全站免登陆