当前位置 : 主页 > 编程语言 > python >

read_excel函数中usecols参数的使用

来源:互联网 收集:自由互联 发布时间:2022-06-15
使用数据框DataFrame类的read_excel函数读取excel表格,生成DataFrame实例,通常是每次进行数据处理的必备操作步骤。多次使用之后,思考了如何从excel表格中获取有用的数据列的问题。通过

使用数据框DataFrame类的read_excel函数读取excel表格,生成DataFrame实例,通常是每次进行数据处理的必备操作步骤。多次使用之后,思考了如何从excel表格中获取有用的数据列的问题。通过关键字参数usecols可以控制读取哪些列到数据框中。结合python文档说明,总结如下:

usecols:int, str, list-like, or callable default None

usecols是关键字参数,它的值可以是数字,字符串,或者是可调用函数。默认值是None。

第一种:None值

If None, then parse all columns.

如果采用默认值,即usecols=None,将读取excel表格中的所有非空列数据到数据框中。

第二种:逗号或冒号分隔英文字母

If str, then indicates comma separated list of Excel column letters and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of both sides.

如果采用字符,可以是逗号分隔开A,B,C,D,E...等字母构成的python字符串,如usecols=“A,C,D,F,J”,不区分大小写。也可以是用冒号连接两个字母表于的列的范围,如usecols=“E:K",代表excel表格E到K之间的所有列,包括E列和K列。在代表字符串的引号内部,也可以是上述两种写法的组合,如usecols="A,C,E:F"

read_excel函数中usecols参数的使用_read_excel

第三种:数字构成的python列表结构

If list of int, then indicates list of column numbers to be parsed.

可以是”基于0“的由数字元素构成的python列表,如usecols=[2,3,6,9]。

read_excel函数中usecols参数的使用_数据处理_02

第四种:字符串构成的python列表结构

If list of string, then indicates list of column names to be parsed.

可以是字符串构成的python列表,如usecols=['英语','数学','美术']。

read_excel函数中usecols参数的使用_python_03

第五种:可调用函数

If callable, then evaluate each column name against it and parse the column if the callable returns True.

usecols还可以是可调用的函数。这时会将每个excel列名字传入函数,将计算结果为True的列读入数据框中。

如usecols=lambda x: '类' in x ,将读人员类别与结算分类两列入院到数据框中。

read_excel函数中usecols参数的使用_read_excel_04

read_excel函数中usecols参数的使用_read_excel_05


上一篇:#yyds干货盘点#python协程
下一篇:没有了
网友评论