簡體   English   中英

Python-Pandas DataFrame中的KeyError

[英]Python - KeyError in Pandas DataFrame

當我導入該數據集時:

dataset = pd.read_csv('lyrics.csv', delimiter = '\t', quoting = 2)

它像這樣打印:

                                 lyrics,classification
0    I should have known better with a girl like yo...
1    You can shake an apple off an apple tree\nShak...
2    It's been a hard day's night\nAnd I've been wo...
3    Michelle, ma belle\nThese are words that go to...
4    Can't buy me love, love\nCan't buy me love\nI'...
5    I love you\nCause you tell me things I want to...
6    I dig a Pygmy by Charles Hawtrey and the Deaf ...
7    The song a robin sings,\nThrough years of endl...
8    Love me tender, love me sweet,\nNever let me g...
9    Well, it's one for the money,\nTwo for the sho...
10   All the words that I let her know\nStill could...

如果我print (dataset.columns) ,我得到:

Index([u'lyrics,classification'], dtype='object')

但是如果我嘗試打印歌詞,就像這樣:

for i in range(0, len(dataset)):
    lyrics=dataset['lyrics'][i]
    print lyrics

我收到以下錯誤:

KeyError: 'lyrics'

我在這里想念什么?

由於將定界符設置為制表符(\\ t),因此不會像您想象的那樣解析標頭。 “歌詞,分類”是一列名稱。 如果要將定界符保留為制表符,那么在歌詞和分類之間應該有一個制表符而不是逗號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM