3

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 2

6

If we already know which column to read

yourdf = pd.read_csv('your file', usecols =[2,3,4,5], header = None)
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.