2

I have this code:

plt.subplot(121)
df=pd.DataFrame()
df['age']=[1,2,3,4,5,6,7,8,9,0,2,3,4,5,6,7,8,9,0,4,1,2,3,4,5,6,7,8,9,0]
df['age_cat']=pd.cut(age,bins=list(np.arange(1, 10, 2)))
df.groupby('age_cat')['age'].count().plot(kind='bar')

but when I run this code, I am getting this error:

TypeError: Cannot cast IntervalIndex to dtype int32

I am trying to plot the histogram of age values. I simplified the code to show the error.

It seems that the error is related to the plot function as if I remove it, the code works.

Edit 1

changed the code to this:

plt.subplot(121)
df=pd.DataFrame()
df['age']=[1,2,3,4,5,6,7,8,9,0,2,3,4,5,6,7,8,9,0,4,1,2,3,4,5,6,7,8,9,0]
df['age_cat']=pd.cut(df['age'],bins=list(np.arange(1, 10, 2)))
df.groupby('age_cat')['age'].count().plot(kind='bar')   

But still the same error message.

My library versions are:

absl-py                0.11.0   
astunparse             1.6.3    
async-generator        1.10     
attrs                  20.3.0   
backcall               0.2.0    
bleach                 3.2.1
cachetools             4.2.0
certifi                2020.12.5
chardet                4.0.0
colorama               0.4.4
cycler                 0.10.0
decorator              4.4.2
defusedxml             0.6.0
entrypoints            0.3
flatbuffers            1.12
gast                   0.3.3
google-auth            1.24.0
google-auth-oauthlib   0.4.2
google-pasta           0.2.0
grpcio                 1.32.0
h5py                   2.10.0
idna                   2.10
ipykernel              5.4.3
ipython                7.19.0
ipython-genutils       0.2.0
jedi                   0.18.0
Jinja2                 2.11.2
joblib                 1.0.0
jsonschema             3.2.0
jupyter-client         6.1.11
jupyter-core           4.7.0
jupyterlab-pygments    0.1.2
Keras-Preprocessing    1.1.2
kiwisolver             1.3.1
Markdown               3.3.3
MarkupSafe             1.1.1
matplotlib             3.3.3
mistune                0.8.4
nbclient               0.5.1
nbconvert              6.0.7
nbformat               5.1.2
nest-asyncio           1.4.3
numpy                  1.19.3
oauthlib               3.1.0
opt-einsum             3.3.0
packaging              20.8
pandas                 1.2.0
pandocfilters          1.4.3
parso                  0.8.1
pickleshare            0.7.5
Pillow                 8.1.0
pip                    20.3.3
prompt-toolkit         3.0.10
protobuf               3.14.0
pyasn1                 0.4.8
pyasn1-modules         0.2.8
Pygments               2.7.4
pyparsing              2.4.7
pyrsistent             0.17.3
python-dateutil        2.8.1
pytz                   2020.5
pywin32                300
pyzmq                  21.0.1
requests               2.25.1
requests-oauthlib      1.3.0
rsa                    4.7
scikit-learn           0.24.0
scipy                  1.6.0
seaborn                0.11.1
setuptools             49.2.1
six                    1.15.0
tabulate               0.8.7
tensorboard            2.4.1
tensorboard-plugin-wit 1.7.0
tensorflow             2.4.0
tensorflow-estimator   2.4.0
termcolor              1.1.0
testpath               0.4.4
tf                     1.0.0
threadpoolctl          2.1.0
tornado                6.1
traitlets              5.0.5
typing-extensions      3.7.4.3
urllib3                1.26.2
wcwidth                0.2.5
webencodings           0.5.1
Werkzeug               1.0.1
wheel                  0.36.2
wrapt                  1.12.1
1

1 Answer 1

1

Looks like you are cutting the wrong thing, should be df['age'] , not age.

df['age_cat']=pd.cut(df['age'],bins=list(np.arange(1, 10, 2)))

Output:

enter image description here


Some side notes:

  1. You don't need to wrap list around np.arange.
  2. You can do value_counts with bins:

df['age'].value_counts(bins=np.arange(1, 10, 2)).plot.bar()

which gives:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Please see my edited question. I made the suggested changes, but still getting the same error.
What's your version of Pandas/Matplotlib?
added to question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.