【问题标题】:TypeError: cannot perform reduce with flexible type while using boxplotTypeError:使用箱线图时无法使用灵活类型执行减少
【发布时间】:2020-08-17 07:37:08
【问题描述】:

我有数据框。

df=df.astype(np.float)
df.head()
x   y
1   0.6 2
0   1.1 1
3   3.4 4
2   4.3 3

我正在尝试使用它生成箱线图。

ax= plt.gca()
ax.boxplot(df[['x','y']])

但出现以下错误: TypeError: cannot perform reduce with flexible type

我无法理解为什么,因为数据框中的所有值都是数字。

【问题讨论】:

  • df 有 2 列还是 3 列? [1,0,3,2] 只是索引?
  • 只有两列
  • 我的错误是:NameError: name 'ax' is not defined
  • @ipj 因为必须使用ax = plt.gca()
  • 打错字了,抱歉。

标签: python pandas matplotlib


【解决方案1】:

错误很容易解释。它无法处理其作为数据框的输入。尽管数据框和数组都有相似的表示形式,但它们有一个本质的区别,即[n for n in df[['x','y']]] 的输出是列名列表(['x','y'])而不是列值。我猜您希望在您的 boxplot 方法中使用值,因此应将 df[['x','y']] 替换为 df[['x','y']].values

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-07
    • 2015-04-08
    • 1970-01-01
    • 2016-09-13
    • 2019-07-04
    • 2012-11-16
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多