【问题标题】:I am getting an error while obtainin countplot获取计数图时出现错误
【发布时间】:2023-01-25 20:51:01
【问题描述】:

这是我的代码

fig, ax = plt.subplots(4,4, figsize=(15,15))
for i,col in enumerate(churn3_01):
    row, col = divmod(i, 4)
    sns.countplot(x=col, data=churn3, ax=ax[row,col], hue='Churn')

churn3 是我的主数据框,churn3_01churn3 列的子集,Churn 是我的churn3 列。我收到此错误:

'bool' object has no attribute 'all'

我想为我的新数据框 churn_01 中的所有列创建一个计数图

【问题讨论】:

  • 你能包括一些可复制的玩具数据吗?你能说出哪一行准确地给出了错误吗?你能包括完整的错误跟踪吗?
  • 请注意,您的变量col 有问题。你给它分配了两个不同的东西:一次在for i,col in enumerate(churn3_01),一次在row, col = divmod(i, 4)。您应该更改其中之一的名称。

标签: python seaborn churn


【解决方案1】:

错误消息“'bool' object has no attribute 'all'”很可能是由enumerate(churn3_01) 循环引起的。 enumerate() 函数用于迭代序列(如列表或元组)并返回每个元素的索引和值。然而,churn3_01 是一个 DataFrame 而不是一个序列。

您可以使用 iteritems() 方法遍历 DataFrame 中的列并获取列名及其对应的数据。以下是如何修改代码以使用 iteritems() 方法的示例:

fig, ax = plt.subplots(4, 4, figsize=(15,15))
for i, (col_name, col_data) in enumerate(churn3_01.iteritems()):
    row, col = divmod(i, 4)
    sns.countplot(x=col_data, data=churn3, ax=ax[row, col], hue='Churn')

这样,iteritems() 方法将遍历数据框的列,col_name 将为您提供列的名称,col_data 将为您提供列的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-21
    • 2017-05-07
    • 2014-07-02
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2020-04-21
    相关资源
    最近更新 更多