pandas.read_excel()读取指定列

2020-06-01  本文已影响0人  lalalasa

pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=False, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=True, mangle_dupe_cols=True, **kwds)

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

1.为空。

If None, then parse all columns.

2.指定Excel列的名字

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

如:panda.read_excel(usecos=[1,3]),表示读取第二、四列

3.指定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.

4.指定数据的列名

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

如usecols="col_name1,col_name2",表示读取列名为col_name1和col_name2这两列

PS:另一种读取指定列的方法:

xl_file = pd.read_excel('D:/SnapPython/TestDF.xlsx', sheet_name='Sheet 2')['ForeignKey']

例如:

先获取整个sheet

df = pd.read_excel(

            file_io,

            sheet_name=sheet1,

            skiprows=[0,1]

        )

再抽取自己需要的列

    df = df[[col1, col2, col3]]
上一篇下一篇

猜你喜欢

热点阅读