Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - I'm getting an error in the following program as KeyError Traceback (most recent call last)

Here is my code-

import pandas as pd

df5=pd.read_csv(r'C:Anaconda3airtravel.csv')

print(df5)

   Month   "1958"   "1959"   "1960"
0    JAN      340      360      417
1    FEB      318      342      391
2    MAR      362      406      419
3    APR      348      396      461
4    MAY      363      420      472
5    JUN      435      472      535
6    JUL      491      548      622
7    AUG      505      559      606
8    SEP      404      463      508
9    OCT      359      407      461
10   NOV      310      362      390
11   DEC      337      405      432

But when I try to access this:-

print(df5['1960'])

I get an error.

KeyError: '1960'

I can't understand why is that happening. File link: https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html

Thanks

question from:https://stackoverflow.com/questions/65941382/im-getting-an-error-in-the-following-program-as-keyerror-traceback-most-recent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your column includes quotations along with the column name. maybe df5['"1960"'] would work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...