【问题标题】:'DataFrame' object has no attribute 'label''DataFrame' 对象没有属性 'label'
【发布时间】:2022-01-11 21:34:23
【问题描述】:

我正在尝试绘制 k-means,但出现此错误:

'DataFrame' object has no attribute 'label'

我找到了this similar question,但它没有给我所需的答案。

print(df.head().to_dict())

{'CUST_ID': {0: 10001, 1: 10002, 2: 10003, 3: 10004, 4: 10005}, 'BALANCE': {0: 40.900749, 1: 3202.467416, 2: 2495.148862, 3: 1666.670542, 4: 817.714335}, 'BALANCE_FREQUENCY': {0: 0.818182, 1: 0.909091, 2: 1.0, 3: 0.636364, 4: 1.0}, 'PURCHASES': {0: 95.4, 1: 0.0, 2: 773.17, 3: 1499.0, 4: 16.0}, 'ONEOFF_PURCHASES': {0: 0.0, 1: 0.0, 2: 773.17, 3: 1499.0, 4: 16.0}, 'INSTALLMENTS_PURCHASES': {0: 95.4, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}, 'CASH_ADVANCE': {0: 0.0, 1: 6442.945483, 2: 0.0, 3: 205.788017, 4: 0.0}, 'PURCHASES_FREQUENCY': {0: 0.166667, 1: 0.0, 2: 1.0, 3: 0.083333, 4: 0.083333}, 'ONEOFF_PURCHASES_FREQUENCY': {0: 0.0, 1: 0.0, 2: 1.0, 3: 0.083333, 4: 0.083333}, 'PURCHASES_INSTALLMENTS_FREQUENCY': {0: 0.083333, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}, 'CASH_ADVANCE_FREQUENCY': {0: 0.0, 1: 0.25, 2: 0.0, 3: 0.083333, 4: 0.0}, 'CASH_ADVANCE_TRX': {0: 0, 1: 4, 2: 0, 3: 1, 4: 0}, 'PURCHASES_TRX': {0: 2, 1: 0, 2: 12, 3: 1, 4: 1}, 'CREDIT_LIMIT': {0: 1000.0, 1: 7000.0, 2: 7500.0, 3: 7500.0, 4: 1200.0}, 'PAYMENTS': {0: 201.802084, 1: 4103.032597, 2: 622.066742, 3: 0.0, 4: 678.334763}, 'MINIMUM_PAYMENTS': {0: 139.509787, 1: 1072.340217, 2: 627.284787, 3: 864.2065423050816, 4: 244.791237}, 'PRC_FULL_PAYMENT': {0: 0.0, 1: 0.222222, 2: 0.0, 3: 0.0, 4: 0.0}, 'TENURE': {0: 12, 1: 12, 2: 12, 3: 12, 4: 12}}
plt.scatter(df["BALANCE"][df.label == 0], df["PURCHASE"][df.label == 0],s=80,c='magenta',label='Careful')
plt.scatter(df["BALANCE"][df.label == 1], df["PURCHASE"][df.label == 1],s=80,c='yellow',label='Standard')
plt.scatter(df["BALANCE"][df.label == 2], df["PURCHASE"][df.label == 2],s=80,c='green',label='Target')
plt.scatter(df["BALANCE"][df.label == 3], df["PURCHASE"][df.label == 3],s=80,c='cyan',label='Careless')
plt.scatter(df["BALANCE"][df.label == 4], df["PURCHASE"][df.label == 4],s=80,c='burlywood',label='Sensible')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red', 
label = 'Centroids')
plt.title('Customers Segmentation based on their Credit Card usage bhaviour')
plt.xlabel('BALANCE')
plt.ylabel('PURCHASE')
plt.legend()
plt.show()

<ipython-input-72-532d227bb220> in <module>
----> 1 plt.scatter(df["BALANCE"][df.label == 0],          
      2             df["PURCHASE"][df.label == 0],s=80,c='magenta',label='Careful')
      3 plt.scatter(df["BALANCE"][df.label == 1],          
      4             df["PURCHASE"][df.label == 1],s=80,c='yellow',label='Standard')
      5 plt.scatter(df["BALANCE"][df.label == 2],          

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5463             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5464                 return self[name]
-> 5465             return object.__getattribute__(self, name)
   5466 
   5467     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'label'

【问题讨论】:

  • 请将print(df.head().to_dict())的结果添加到问题中。 :)
  • df.head().to_dict()结果,而不是代码本身。您需要在您提供的代码(引发错误的代码)之前而不是之后添加此代码。
  • 是的@Mohamed,我的意思是你应该执行该代码并粘贴它的 输出,正如 Quang 所说。
  • 所以你的数据框中没有label 列。
  • 另外你在这行有一个语法错误:label = 'Centroids')

标签: python pandas dataframe


【解决方案1】:

错误 'DataFrame' object has no attribute 'label' 试图告诉您您的代码正在向 DataFrame 询问名为“label”的列,但问题是:您该 DataFrame 中没有名为“label”的列

那么你真正想使用哪一列?

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2021-12-17
    • 2021-07-14
    • 2013-10-23
    • 2017-10-22
    • 2021-01-08
    • 2021-11-01
    • 2016-03-09
    相关资源
    最近更新 更多