【问题标题】:Skipping range of rows after header through pandas.read_excel通过 pandas.read_excel 在标题后跳过行范围
【发布时间】:2018-09-22 20:56:54
【问题描述】:

我知道pandas.read_excel() 中的参数usecols 允许您选择特定的列。

假设我用pandas.read_excel() 读入了一个 Excel 文件。我的 excel 电子表格有 1161 行。我想保留第一行(索引为 0),并跳过第 2:337 行。似乎参数 skiprows 仅在涉及 0 索引时才有效。我不知道我是否错了,但我的代码的几次运行总是产生读取我的 1161 行的 all 的输出,而不是仅在第 337 行之后。比如这样:

documentationscore_dataframe = pd.read_excel("Documentation Score Card_17DEC2015 Rev 2 17JAN2017.xlsx",
                                        sheet_name = "Sheet1",
                                        skiprows = "336",
                                        usecols = "H:BD")

这是我设置的另一种尝试。

documentationscore_dataframe = pd.read_excel("Documentation Score Card_17DEC2015 Rev 2 17JAN2017.xlsx",
                                        sheet_name = "Sheet1",
                                        skiprows = "1:336",
                                        usecols = "H:BD")

我希望数据框在原始 Excel 导入中排除第 2 到 337 行。

【问题讨论】:

    标签: python excel pandas dataframe


    【解决方案1】:

    根据documentationpandas.read_excelskiprows 必须类似于列表。

    试试这个来排除第 1 到 336 行(含):

    df = pd.read_excel("file.xlsx",
                       sheet_name = "Sheet1",
                       skiprows = range(1, 337),
                       usecols = "H:BD")
    

    注意range 构造函数被视为类似list,因此不需要显式的列表转换。

    【讨论】:

    • +1 表示它类似于列表,解决了我阅读标题但跳过第一行之后的问题(pd.read_excel(path, skiprows=[1])
    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多