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 p
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)
[Corresponding documentation][1]:
usecolsint, str, list-like, or callable default None
- 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.
New in version 0.24.0.
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.
New in version 0.24.0.
"usecols" should help, use range of columns (as per excel worksheet, A,B...etc.) below are the examples
df = pd.read_excel(file_location,sheet_name='Sheet1', usecols="A,C,F")
df = pd.read_excel(file_location,sheet_name='Sheet1', usecols="A:F,H")
df = pd.read_excel(file_location,sheet_name='Sheet1', usecols="A:F,H,J:N")
df = pd.read_excel(file_location,sheet_name='Sheet1', usecols="A:N")
parse_cols
is deprecated, use usecols
instead
that is:
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols = "A,C:AA")