关键错误: [Int64Index...] dtype='int64] 中的任何一列不在列中

10 浏览
0 Comments

关键错误: [Int64Index...] dtype='int64] 中的任何一列不在列中

我正在尝试使用np.random.shuffle()方法来对我的索引进行洗牌,但我一直收到一个我不理解的错误。如果有人能帮我弄清楚这个问题,我将不胜感激。谢谢!

我尝试在开始时使用delimiter=','和delim_whitespace=0来创建我的raw_csv_data变量,因为我看到这是解决另一个问题的方法,但它一直抛出相同的错误。

import pandas as pd 
import numpy as np 
from sklearn.preprocessing import StandardScaler
raw_csv_data= pd.read_csv('Absenteeism-data.csv')
df= raw_csv_data.copy()
pd.options.display.max_columns=None
pd.options.display.max_rows=None
df=df.drop(['ID'], axis=1)
targets= np.where(df['Absenteeism Time in Hours']>df['Absenteeism Time in Hours'].median(),1,0)
df['Excessive Absenteeism']= targets
df['Date']= pd.to_datetime(df['Date'], format='%d/%m/%Y')
list_months=[]
for i in range(df.shape[0]):
    list_months.append(df['Date'][i].month)
df['Month Value']= list_months
df['Day of the Week']= df['Date'].apply(date_to_weekday)
df= df.drop(['Date'], axis=1)
reordered_columns= ['Reason for Absence', 'Month Value','Day of the Week','Transportation Expense', 'Distance to Work', 'Age','Daily Work Load Average', 'Body Mass Index', 'Education', 'Children','Pets','Absenteeism Time in Hours', 'Excessive Absenteeism']
df=df[reordered_columns]
df_date_mod= df.copy()
unscaled_inputs= df_date_mod.loc[:, ['Month Value','Day of the Week','Transportation Expense','Distance to Work','Age','Daily Work Load Average','Body Mass Index','Children','Pets','Absenteeism Time in Hours']]
absenteeism_scaler= StandardScaler()
absenteeism_scaler.fit(unscaled_inputs)
scaled_inputs= absenteeism_scaler.transform(unscaled_inputs)
scaled_inputs= pd.DataFrame(scaled_inputs, columns=['Month Value','Day of the Week','Transportation Expense','Distance to Work','Age','Daily Work Load Average','Body Mass Index','Children','Pets','Absenteeism Time in Hours'])
df_date_mod= df_date_mod.drop(['Month Value','Day of the Week','Transportation Expense','Distance to Work','Age','Daily Work Load Average','Body Mass Index','Children','Pets','Absenteeism Time in Hours'], axis=1)
df_date_mod=pd.concat([df_date_mod,scaled_inputs], axis=1)
df_date_mod= df_date_mod[reordered_columns]
df_date_scale_mod= df_date_mod.copy()
reason_columns=pd.get_dummies(df_date_scale_mod['Reason for Absence'], drop_first=True)
df_date_scale_mod= df_date_scale_mod.drop(['Reason for Absence'], axis=1)
reason_type_1= reason_columns.loc[:, 1:14].max(axis=1)
reason_type_2= reason_columns.loc[:, 15:17].max(axis=1)
reason_type_3= reason_columns.loc[:, 18:21].max(axis=1)
reason_type_4= reason_columns.loc[:, 22:].max(axis=1)
df_date_scale_mod= pd.concat([df_date_scale_mod, reason_type_1,reason_type_2, reason_type_3, reason_type_4], axis=1)
column_names= ['Month Value','Day of the Week','Transportation Expense','Distance to Work','Age','Daily Work Load Average','Body Mass Index','Education','Children','Pets','Absenteeism Time in Hours','Excessive Absenteeism', 'Reason_1', 'Reason_2', 'Reason_3', 'Reason_4']
df_date_scale_mod.columns= column_names
column_names_reordered= ['Reason_1', 'Reason_2', 'Reason_3', 'Reason_4','Month Value','Day of the Week','Transportation Expense','Distance to Work','Age','Daily Work Load Average','Body Mass Index','Education','Children','Pets','Absenteeism Time in Hours','Excessive Absenteeism']
df_date_scale_mod=df_date_scale_mod[column_names_reordered]
df_date_scale_mod_reas= df_date_scale_mod.copy()
df_date_scale_mod_reas['Education']= df_date_scale_mod_reas['Education'].map({1:0, 2:1, 3:1, 4:1})
df_preprocessed= df_date_scale_mod_reas.copy()
scaled_inputs_all= df_preprocessed.loc[:,'Reason_1':'Absenteeism Time in Hours']
targets_all= df_preprocessed.loc[:,'Excessive Absenteeism']
shuffled_indices= np.arange(scaled_inputs_all.shape[0])
np.random.shuffle(shuffled_indices)
shuffled_inputs= scaled_inputs_all.iloc[shuffled_indices]
shuffled_targets= targets_all.iloc[shuffled_indices]

这是我在尝试洗牌索引时一直收到的错误:

KeyError Traceback (most recent call last)

in

1 shuffled_indices= np.arange(scaled_inputs_all.shape[0])

2 np.random.shuffle(shuffled_indices)

----> 3 shuffled_inputs= scaled_inputs_all[shuffled_indices]

4 shuffled_targets= targets_all[shuffled_indices]

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)

2932 key = list(key)

2933 indexer = self.loc._convert_to_indexer(key, axis=1,

-> 2934 raise_missing=True)

2935

2936 # take() does not accept boolean indexers

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)

1352 kwargs = {'raise_missing': True if is_setter else

1353 raise_missing}

-> 1354 return self._get_listlike_indexer(obj, axis, **kwargs)[1]

1355 else:

1356 try:

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)

1159

1160 self._validate_read_indexer(keyarr, indexer,

-> 1161 raise_missing=raise_missing)

1162 return keyarr, indexer

1163

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)

1244 raise KeyError(

1245 u"None of [{key}] are in the [{axis}]".format(

-> 1246 key=key, axis=self.obj._get_axis_name(axis)))

1247

1248 # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [Int64Index([560, 320, 405, 141, 154, 370, 656, 26, 444, 307,\n ...\n 429, 542, 676, 588, 315, 284, 293, 607, 197, 250],\n dtype='int64', length=700)] are in the [columns]"

0