brittAnderson / Intro2Computing4Psychology

A guided introduction to computing tools useful for research in psychology - targeted to complete beginners
47 stars 159 forks source link

Pandas Asgt -- Key Error #43

Closed n96khan closed 3 years ago

n96khan commented 3 years ago

I keep getting a KeyError when I try to print the length of one row in my data set. I saw on the python website that KeyError might be an error in the zipfile rather than my code, so I tried it with 2 other files, but I continue to get the same error message. For reference, this is what I have so far:

import requests
import zipfile
import pandas as pd

url = "http://openpsychometrics.org/_rawdata/WPI.zip"
request_file = requests.get(url)
filename = url.split('/')[-1]
with open(filename, 'wb') as output_file:
    output_file.write(request_file.content)

with zipfile.ZipFile('./WPI.zip', 'r') as zip_ref:
    zip_ref.extractall('.')

dpd = pd.read_csv("./WPI/data.csv")
print ("the name of the columns are as following: '\n'"
    + ('\n'.join(dpd.columns.values)))

# what is the length of one row?
#   Currently trying...
print("the length of one of the rows is:"
    + str(len(dpd['Q102'])) + "\n")

To which I get: KeyError 'Q102'