We help IT Professionals succeed at work.

Why is this code wrong?

Maria Qureshi
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80]

df['AgeBin'] = pd.cut(df['Age'], bins)

NameError                                 Traceback (most recent call last)
<ipython-input-16-0750c69cbf65> in <module>()
      1 bins = [0, 10, 20, 30, 40, 50, 60, 70, 80]
----> 3 df['AgeBin'] = {pd.cut(df['Age'], bins)}

NameError: name 'pd' is not defined
Watch Question

you need to tell python what pd is before you can use it.
Are you working your way through some tutorial? If so, you may have missed a few previous lines.
pd may be the pandas module as in
import pandas as pd

Open in new window

But what then is df? It seems to be dict-like but again is not defined in the code you posted.


df is data frame  (my set of data)
NorieAnalyst Assistant

So you are using pandas?
So you are using pandas?
It sure looks like she is.
Code in the following pages use pd, df, and bins: