2

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?

o-90
  • 17,045
  • 10
  • 39
  • 63
frank99
  • 21
  • 1
  • 3

2 Answers2

3

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')
mech
  • 2,775
  • 5
  • 30
  • 38
Mike8
  • 61
  • 5
1

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

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252