1

I'm working with CSV file with over million entries. I'm trying to read data for each candidate as a separate column. I was able to parse data for the first candidate but when I get to next candidate I'm getting error ['cand_nm'] not in index.

Here is the link to file: Link to data file

%matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

First candidate

reader_bachmann = pd.read_csv('P00000001-ALL.csv', squeeze=True, low_memory=False, nrows=411 )
print(reader_bachmann)
cand_bachmann = reader_bachmann[["cand_nm"]] 
print(cand_bachmann)

Second candidate

reader_romney = pd.read_csv('P00000001-ALL.csv', skiprows=range(0,411) ,  na_filter =True, low_memory=False)
print(reader_romney)
cand_romney = reader_romney[["cand_nm"]] 
print(cand_romney)

Error message

KeyError: "['cand_nm'] not in index"
2
  • 1
    Is the first row the headers? It seems your skiprows=range(0,411) would have skipped the headers, causing 'cand_nm' to be undefined index Commented Apr 18, 2017 at 18:07
  • Thank you. It fixed the problem. Commented Apr 18, 2017 at 18:11

1 Answer 1

2

When you use skip_rows like that you lose the header. So you header for reader_romney is now row number 412. If this is the way you want to read the file you will need to store the header line to a list of strings and then pass that list as the names= kwarg. For example

r_bachman = pd.read_csv('P00000001-ALL.csv', squeeze=True, low_memory=False,
                        nrows=411 )
cols = r_bachman.columns
r_romney = pd.read_csv('P00000001-ALL.csv', skiprows=range(0,411),
                       na_filter =True, low_memory=False, names=cols)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It fixed the problem.

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.