python - 绘制直方图 : TypeError: cannot perform reduce with flexible type

标签 python matplotlib histogram

我有一个数据框,我需要绘制一列的直方图,但 IO 不断出现错误。这是我的代码:

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()  

我不断收到错误 TypeError: cannot perform reduce with flexible type 我不知道为什么。

谢谢。

最佳答案

plt.hist不需要 DataFrame。它需要“(n,) 个数组或 (n,) 个数组的序列”。

改为:

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

您也可以这样做,因为 pd.Seriesplt.hist 一起使用:

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

关于python - 绘制直方图 : TypeError: cannot perform reduce with flexible type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665460/

相关文章:

python - 如何使用python-mock库

Python:检查列表是否可以通过交换两个元素来排序,只允许交换一次

python - 删除后如何恢复图形的导航工具栏?

c# - .net 中的 binning-bucketing 数值

python - 多维数组上的 Numpy 直方图

python - 使用 Python 修复 HTML 字符串中的开括号问题

python - 如何使用距离矩阵和 kmedoids 将新观测值分配给聚类?

python - scikit-learn 中回归交叉验证的递归特征消除

matplotlib - 删除 matplotlib 中的误差条上限

bash - gnuplot 直方图 : line 0: Too many columns in using specification