【问题标题】:How to read specific range from Excel sheet [duplicate]如何从 Excel 工作表中读取特定范围 [重复]
【发布时间】:2022-12-04 17:29:48
【问题描述】:

我想从特定单元格范围读取值,即 A2:B10(例如)到我的 python 环境中,而不是读取整张纸。

pandas 是否有直接可用的方法?

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    是的,您可以使用 pandas.read_excel() 函数从 Excel 文件中读取特定单元格或单元格范围。您可以使用 usecols 参数指定要读取的单元格范围。 usecols参数接受一个字符串指定要读取的单元格范围,使用Excel格式A2:B10表示从第二行第一列(A)到第十行第二列(B)的单元格。

    以下是如何使用 pandas.read_excel() 函数从 Excel 文件中读取特定单元格范围的示例:

    import pandas as pd
    
    # Read the Excel file and select the cells in the range A2:B10
    df = pd.read_excel("data.xlsx", usecols="A2:B10")
    

    df 变量现在将包含一个数据框,其中包含指定范围内的单元格的值。然后,您可以像处理 pandas 中的任何其他数据帧一样处理数据帧中的数据。

    【讨论】:

    • 谢谢。但是我收到错误消息 ValueError: Invalid column name: A2
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多