user7830303
user7830303

Reputation:

Python - KeyError in Pandas DataFrame

when I import this dataset:

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

it prints like so:

                                 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...

and if I print (dataset.columns), I get:

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

but if I try to prints the lyrics, like so:

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

I get the following error:

KeyError: 'lyrics'

what am I missing here?

Upvotes: 0

Views: 1121

Answers (1)

dataxerik
dataxerik

Reputation: 28

Since you set the delimiter to be a tab (\t), the header isn't be parsed as you think. 'lyrics,classification' is one column name. If you want to keep the delimiter as a tab, then between lyrics and classification there should be a tab rather than a comma.

Upvotes: 1

Related Questions