簡體   English   中英

關鍵錯誤:“[Int64Index...] dtype='int64] 均不在列中”

[英]Key Error: "None of [Int64Index...] dtype='int64] are in the columns"

代碼:-

import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


class Vizualizer:

    def __init__(self,data,cols):
        self.data=data
        self.cols=cols

    def box_plot(self):
            for col in self.cols:
                sns.boxplot(x=col,y='Ex-Showroom_Price',data=self.data[self.data['Ex-Showroom_Price']])


    

df=pd.read_csv('cars_engage_2022.csv')
cols=['Make','Model','City_Mileage','Highway_Mileage','ARAI_Certified_Mileage','Type','Fuel_Type','Body_Type']
d=Vizualizer(df,cols)
df["Ex-Showroom_Price"] = df["Ex-Showroom_Price"].str.replace(r'.* ([\d,]+)+$', r'\1',regex=True).str.replace(',', '',regex=True).astype('int32')
d.box_plot()

錯誤

Traceback (most recent call last):
  File "c:\Users\shweta\OneDrive\Desktop\DEMO\demo\src\carsengage2022\preprocessing\roughVIZ.py", line 36, in <module>
    d.box_plot()
  File "c:\Users\shweta\OneDrive\Desktop\DEMO\demo\src\carsengage2022\preprocessing\roughVIZ.py", line 25, in box_plot
    sns.boxplot(x=col,y='Ex-Showroom_Price',data=self.data[self.data['Ex-Showroom_Price']])
  File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3511, in __getitem__
    indexer = self.columns._get_indexer_strict(key, "columns")[1]
  File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 5782, in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)
  File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 5842, in _raise_if_missing
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Int64Index([ 292667,  236447,  296661,  334768,  272223,  314815,  279650,\n             351832,  333419,  362000,\n            ...\n    
        1065900, 1182000, 1312000, 1111000, 1191000, 1302000, 1421000,\n            1431000, 1201000, 6862560],\n           dtype='int64', length=1276)] are 
in the [columns]"

我正在嘗試在 cols 的列和它們的 Ex-Showroom-Price 之間繪制一個箱線圖。 Ex-Showroom-Price 的值是分類的,因此,我首先將它們轉換為整數,然后我試圖做箱線圖,但它拋出的錯誤是,Key Error: "None of [Int64Index...] dtype='int64] 在列中”。

我認為您需要交換這些行

df["Ex-Showroom_Price"] = df["Ex-Showroom_Price"].str.replace(r'.* ([\d,]+)+$', r'\1',regex=True).str.replace(',', '',regex=True).astype('int32')
d=Vizualizer(df,cols)

或者通過以下方式確保您正在編輯類中的數據框:

d=Vizualizer(df,cols)
d.df["Ex-Showroom_Price"] = df["Ex-Showroom_Price"].str.replace(r'.* ([\d,]+)+$', r'\1',regex=True).str.replace(',', '',regex=True).astype('int32')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM