我有一个数据框,我需要绘制一列的直方图,但 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.Series
与 plt.hist
一起使用:
plt.hist(df['col'], bins=bins)
关于python - 绘制直方图 : TypeError: cannot perform reduce with flexible type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665460/