如何正确设置read_excel函数的usecols参数以读取特定列?

2026-05-28 17:431阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计536个文字,预计阅读时间需要3分钟。

如何正确设置read_excel函数的usecols参数以读取特定列?

使用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参数以读取特定列?

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

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

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

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

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

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

第五种:可调用函数

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 ,将读人员类别与结算分类两列入院到数据框中。


标签:使

本文共计536个文字,预计阅读时间需要3分钟。

如何正确设置read_excel函数的usecols参数以读取特定列?

使用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参数以读取特定列?

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

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

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

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

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

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

第五种:可调用函数

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 ,将读人员类别与结算分类两列入院到数据框中。


标签:使