frank99
frank99

Reputation: 21

Pandas not in index error trying a scatter plot

I was trying to do a scatter plot. I was trying with the next code

df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
df.columns = ['a', 'b']
df
df.plot.scatter(df['a'], df['b'])

I get the error

KeyError: '[1 2] not in index'

Any idea why this happens?

Upvotes: 2

Views: 3151

Answers (2)

Mike8
Mike8

Reputation: 61

This line is redundant:

df.plot.scatter(df['a'], df['b'])

Since you've already called df, you only need to refer to the column heading, like so:

df.plot.scatter('a', 'b')

Upvotes: 3

jezrael
jezrael

Reputation: 862651

First no problem you are new in python ;)

Need parameter x and y in DataFrame.plot.scatter:

df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
df.columns = ['a', 'b']

df.plot.scatter(x = 'a', y='b')

graph

Upvotes: 1

Related Questions