pandas 将数据从excel导入Python时排除第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45892612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:19:53  来源:igfitidea点击:

Exclude first row when importing data from excel into Python

pythonpandasimport-from-excel

提问by Nguyen

I have a partial code to import excel into Python as strings. How I can exclude first row when importing data from excel into Python?

我有部分代码可以将 excel 作为字符串导入 Python。将数据从 excel 导入 Python 时如何排除第一行?

import pandas as pd
data = pd.read_excel(".xlsx", parse_cols="A,C,E,G, I, K, M, O, Q, S, U, W, Y, AA, AC, AE, AG, AI, AK, AM, AO, AQ, AS, AU, AW, AY, BA, BC, BE, BG, BI, BK, BM, BO, BQ, BS, BU, BW, BY, CA, CC, CE, CG, CI, CK, CM, CO, CQ, CS, CU, CW, CY, DA, DC, DE, DG, DI, DK, DM, DO, DQ, DS, DU, DW, DY, EA, EC, DE, EG, EI, EK, EM, EO, EQ, ES, EU, EW, EY")
data = data.to_string()

回答by Onel Harrison

The pandas documentation for the pd.read_excelmethod mentions a skiprowsparameter that you can use exclude the first row of your excel file.

pd.read_excel方法的 Pandas 文档提到了一个skiprows参数,您可以使用该参数排除 excel 文件的第一行。

Example

例子

import pandas as pd
data = pd.read_excel("file.xlsx", parse_cols="A,C,E,G", skiprows=[0])

Source: pandas docs

资料来源:Pandas文档

回答by Max

for read_excel function assign value to skiprowsargument. it will ignore the header

对于 read_excel 函数,为skiprows参数赋值。它会忽略标题

回答by BoB

parse_colsargument is deprecated since version 0.21.0. Instead you should use usecols:

parse_cols参数自 0.21.0 版起已弃用。相反,您应该使用usecols

usecols : int or list, default None

usecols : int 或 list,默认无

  • If None then parse all columns, If int then indicates last column to
  • be parsed If list of ints then indicates list of column numbers to be
  • parsed If string 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.
  • 如果 None 则解析所有列,如果 int 则表示最后一列
  • 被解析如果整数列表则指示要被解析的列号列表
  • parsed If string then 表示 Excel 列的逗号分隔列表
  • 字母和列范围(例如“A:E”或“A、C、E:F”)。范围是
  • 包括双方。

To exclude first row use skiprows=[0] argument.

要排除第一行,请使用skiprows=[0] 参数。