connectaditya / KPMG-Virtual-Internship

kpmg virtual internship solution
39 stars 45 forks source link

Keyerror #1

Open sabariask opened 4 years ago

sabariask commented 4 years ago

Hello sir., I have an KeyError in Line 12 and 13 and i solved this problem. you update the repository please check it...

KeyError Traceback (most recent call last) in --->23 jjj = 1 --->24 for each in df['gender']: ---> 25 temp = int(df['age'][jjj]) ---> 26 bkt = int(df['past_3_years_bike_related_purchases'][jjj]) ---> 27 if each[0] == "Female" and temp != 0:

~\anaconda3\lib\site-packages\pandas\core\series.py in getitem(self, key) ---> 869 key = com.apply_if_callable(key, self) ---> 870 try: --> 871 result = self.index.get_value(self, key) ---> 872 ---> 873 if not is_scalar(result):

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) ---> 4403 k = self._convert_scalar_indexer(k, kind="getitem") ---> 4404 try: ---> 4405 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None)) ---> 4406 except KeyError as e1: ---> 4407 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 4000

iamkumar-gaurav commented 4 years ago

age_dict = {} f1 = [] bf1 = [] f2 = [] bf2 = [] f3 = [] bf3 = [] f4 = [] bf4 = [] m1 = [] bm1 = [] m2 = [] bm2 = [] m3 = [] bm3 = [] m4 = [] bm4 = [] fq = mean_val - stdv_val/2 sq = mean_val tq = mean_val + stdv_val/2 print(fq, sq, tq) jjj = 1 for each in df['gender']: temp = int(df['Age'][jjj]) bkt = int(df['3y_bike_purchases'][jjj]) if each[0] == "F" and temp != 0: if(temp <= fq): f1.append(temp) bf1.append(bkt) elif(fq < temp and temp <= sq): f2.append(temp) bf2.append(bkt) elif(sq < temp and temp <= tq): f3.append(temp) bf3.append(bkt) elif(tq < temp): f4.append(temp) bf4.append(bkt) elif each[0] == "M" and temp != 0: if(temp <= fq): m1.append(temp) bm1.append(bkt) elif(fq < temp and temp <= sq): m2.append(temp) bm2.append(bkt) elif(sq < temp and temp <= tq): m3.append(temp) bm3.append(bkt) elif(tq < temp): m4.append(temp) bm4.append(bkt) jjj += 1 dtt = {"Female1":f1, "Female2":f2, "Female3":f3, "Female4":f4, "Male1":m1, "Male2":m2, "Male3":m3, "Male4":m4, }

just make sure all the packages are imported correctly.it seems like u miss something in Pandas