Plotting Histogram: TypeError: Cannot Prune with Flexible Type

I have a dataframe and I need to plot a column bar chart, but IO keeps getting error. Here is my code:

import pandas as pd  
import matplotlib.pyplot as plt  
import numpy as np  

    data = df[['col']]  
    data.info()>>>  
    RangeIndex: 183404 entries, 0 to 183403
    Data columns (total 1 columns):
    col    183404 non-null int64
    dtypes: int64(1)  

    bins = np.arange(-100, 100, 5)  
    plt.hist(data , bins = bins)    
    plt.show()  

      

I keep getting TypeError: Can't shortcut with flexible type and I don't know why.

Thank.

+3


source to share


2 answers


plt.hist

doesn't want DataFrame. He wants "(n,) array or sequence (n,) arrays".

Instead, do:

plt.hist(data.values, bins=bins)

      



Alternatively, you can simply do this, since it pd.Series

works with plt.hist

:

plt.hist(df['col'], bins=bins)

      

+1


source


There is also a DataFrame built in to a function to do this:

df.hist('col', bins = 5)

      



Bins may work differently than you expect, see the documentation. It accepts an int for the number of boxes (default 10)

0


source







All Articles