如何使用Pandas从Excel中读取某些列-Python [英] how to read certain columns from Excel using Pandas - Python

查看:4125
本文介绍了如何使用Pandas从Excel中读取某些列-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel工作表中读取,并且我想读取某些列:列0(因为它是行索引)和列22:37.现在这是我的工作:

I am reading from an Excel sheet and I want to read certain columns: column 0 because it is the row-index, and columns 22:37. Now here is what I do:

import pandas as pd
import numpy as np
file_loc = "path.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], parse_cols = 37)
df= pd.concat([df[df.columns[0]], df[df.columns[22:]]], axis=1)

但是我希望有更好的方法来做到这一点!我知道我是否可以做到,但是对于大型数据集,这没有任何意义.

But I would hope there is better way to do that! I know if I do parse_cols=[0, 22,..,37] I can do it, but for large datasets this doesn't make sense.

我也这样做了

s = pd.Series(0)
s[1]=22
for i in range(2,14):
    s[i]=s[i-1]+1
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], parse_cols = s)

但是它读取的前15列是s的长度.

But it reads the first 15 columns which is the length of s.

推荐答案

您可以像这样使用列索引(字母):

You can use column indices (letters) like this:

import pandas as pd
import numpy as np
file_loc = "path.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols = "A,C:AA")
print(df)

[相应的文档] [1]:

[Corresponding documentation][1]:

usecolsint str,类似列表或可调用的默认无

  • 如果为None,则解析所有列.
  • 如果为str,则表示以逗号分隔的Excel列字母和列范围的列表(例如"A:E"或"A,C,E:F").范围包括双方.
  • 如果为int列表,则表示要解析的列号列表.
  • 如果为字符串列表,则表示要解析的列名称列表.

  • If None, then parse all columns.
  • 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.
  • If list of int, then indicates list of column numbers to be parsed.
  • If list of string, then indicates list of column names to be parsed.

0.24.0版中的新功能.

New in version 0.24.0.

如果可调用,则根据它评估每个列的名称,如果可调用的返回True,则解析该列.

If callable, then evaluate each column name against it and parse the column if the callable returns True.

根据上述行为返回列的子集.

Returns a subset of the columns according to behavior above.

0.24.0版中的新功能.

New in version 0.24.0.

这篇关于如何使用Pandas从Excel中读取某些列-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆