如何使用 pandas 转换Excel文件中的所有列 [英] How can I convert all columns from my Excel file using pandas

查看:151
本文介绍了如何使用 pandas 转换Excel文件中的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的excel文件中的所有列(59列)转换为数据框,并指定类型。
有些列是字符串,其他日期,其他int等等。
我知道我可以在read_excel方法中使用转换器。
,但是我有很多列,我不想写converter = {'column1':type1,'column2':type2,...,'column59':type59}

I want to convert all columns (59 columns) from my excel file to a dataframe, specifying the types. Some columns are a string, others dates, other int and more. I know I can use a converter in a read_excel method. but I have a lot of columns and I don't want write converter={'column1': type1, 'column2': type2, ..., 'column59': type59}

我的代码是:

import numpy as np
import pandas as pd
import recordlinkage
import xrld

fileName = 'C:/Users/Tito/Desktop/banco ZIKA4.xlsx'
strcols = [0, 5, 31, 36, 37, 38, 39, 40, 41, 45]
datecols = [3, 4, 29, 30, 32, 48, 50, 51, 52, 53, 54, 55]
intcols = [33, 43, 59]
booleancols = [6, ..., 28]
df = pd.read_excel(fileName, sheet_name=0, true_values=['s'], false_values=['n'], converters={strcols: str, intcols: np.int, booleancols: np.bool, datecols: pd.to_datetime})
print(df.iat[1, 31], df.iat[1, 32], df.iat[1, 33])


推荐答案

因为您的代码不起作用,因为转换器 kwarg不允许将多列的列表用作函数的键。

Iiuc your code doesn't work because the converters kwarg doesn't allow lists of several columns as keys to functions.

您可以做的是创建字典而不是o f列出并把连接的字典提供给转换器

What you can do is to create dicts instead of lists and provide the concatenated dicts to converters:

strcols = {c: str for c in [0, 5, 31, 36, 37, 38, 39, 40, 41, 45]}
datecols = {c: pd.to_datetime for c in [3, 4, 29, 30, 32, 48, 50, 51, 52, 53, 54, 55]}
intcols = {c: np.int for c in [33, 43, 59]}
booleancols = {c: np.bool for c in range(6, 29)}
conv_fcts = {**strcols, **datecols, **intcols, **booleancols}

df = pd.read_excel(fileName, converters=conv_fcts, sheet_name=0, true_values=['s'], false_values=['n'])

这篇关于如何使用 pandas 转换Excel文件中的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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