简体   繁体   中英

'DataFrame' object has no attribute 'label'

I'm trying to plot k-means but I get this error:

'DataFrame' object has no attribute 'label'

I found this similar question , but it does not give me the answer I need.

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'

The error 'DataFrame' object has no attribute 'label' is trying to tell you that your code is asking the DataFrame for a column called "label" but the issue is: you don't have a column in that DataFrame called 'label' .

So which column do you actually want to use?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM