Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting a histogram: TypeError: cannot perform reduce with flexible type

I have a data frame and I need to plot a histogram of a column, but IO keep getting an 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 the error TypeError: cannot perform reduce with flexible type and I do not know why.

Thank you.


1 Answers

plt.hist doesn't want a DataFrame. It wants "(n,) array or sequence of (n,) arrays".

Instead do:

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

Alternately you could just do this, as pd.Series work with plt.hist:

plt.hist(df['col'], bins=bins)
like image 66
mechanical_meat Avatar answered Jun 09 '26 07:06

mechanical_meat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!