I have a csv file with hundreds of columns, each column with long names. Is there a way to read only the specified columns by number like column number 2 to 5 and column number 50 to 71. I know I can read specified columns by the column names using the 'usecols' parameter but can I specify the column numbers to get the desired columns?
2 Answers
You can read the whole csv in pandas to avoid any confusions:
df = pd.read_csv(filename)
Then use iloc to get the specific columns, like this:
df.iloc[:, 2:6] # This will give you all rows for columns 2 to 5
OR, if you want to filter directly while reading from csv
From the pd.read_csv docs:
Return a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz'].
You can use usecols with indexes(numbers) or strings.