6

Is there some way of reading only a particular column with specific index from a csv file using Pandas(preferably read_csv)? I understand that read_csv provides the ability to read specific columns by column names, but the data file has no headers so I cannot use column names. Note that the file is too large, so I do not want to read in the entire file and then subset. Thanks.

3
  • 1
    use_cols supports ordinal based indexing: use_cols=[1,4] will read only 2nd and 5th column Commented Sep 7, 2015 at 15:06
  • Many thanks. It works. Commented Sep 7, 2015 at 15:49
  • Just a 'for information' - you can't combine index and name e.g. use_cols = ['Name', 5, 'Date' does not work]. It returns an error 'ValueError: 'usecols' must either be all strings, all unicode, all integers or a callable'. Commented Mar 8, 2019 at 10:03

2 Answers 2

6

Here is an example illustrating the answer given by EdChum. There is a lot of additional options to load a CSV file, check the API reference.

raw_data = {'first_name': ['Steve', 'Guido', 'John'],
        'last_name': ['Jobs', 'Van Rossum', "von Neumann"]}
df = pd.DataFrame(raw_data)
# Saving data without header
df.to_csv(path_or_buf='test.csv', header=False)
# Telling that there is no header and loading only the first name
df = pd.read_csv(filepath_or_buffer='test.csv', header=None, usecols=[1], names=['first_name'])
df

  first_name
0      Steve
1      Guido
2       John
Sign up to request clarification or add additional context in comments.

Comments

4
import pandas as pd
data = pd.read_csv('file.csv', usecols=['column_name'])

Parameter of usecols contain list of column name(s). If want more than one columns,then separate them by comma i.e., ['column_name1, 'column_name2', 'column_name3']

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.