简体   繁体   中英

Read csv file by column number in pandas python

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?

If we already know which column to read

yourdf = pd.read_csv('your file', usecols =[2,3,4,5], header = None)

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM