KeyError Traceback using for+if in python - python

I am a newbie on python. I am trying to check the results of a test and train and I have to compare my predictions with the actual test results (data_train).
Data_train is a dictionary as shown in the image below . The prediction is an array like this
The code aims to counts the consistent classifications between prediction and test results.
consistent=0
inconsistent=0
​
for i in np.linspace(1,len_test,len_test):
if data_train['class'][i] == predictions[i]:
consistent=consistent+1
else:
inconsistent=inconsistent+1
I have this error, what does it means? I don't know how to catch error like this one in python
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~\anaconda4\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\anaconda4\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 1
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10076/1764309407.py in <module>
1 for i in np.linspace(1,len_test,len_test):
----> 2 a=data_train['class'][i]
3 b=predictions[i]
4 if a == b:
5 consistent=consistent+1
~\anaconda4\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
940
941 elif key_is_scalar:
--> 942 return self._get_value(key)
943
944 if is_hashable(key):
~\anaconda4\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
1049
1050 # Similar to Index.get_value, but we do not fall back to positional
-> 1051 loc = self.index.get_loc(label)
1052 return self.index._get_values_for_loc(self, loc, label)
1053
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 1.0

I'm not entirely sure of the code's context, but it seems you're using NumPy. As I understand it, referencing an array element can only be done using an integer (or slices, ellipses etc.), but not strings. You appear to be attempting to reference using the string 'class' and I don't understand why. My suggestion would be to remove the ['class'] reference and simply use the integer reference, such as: a=data_train[i]

Related

KeyError: 0 The above exception was the direct cause of the following exception:

The code was working correctly, but problems appeared in other parts, and when solving the problems of the other parts, this part did not work and I was not able to solve this problem and I hope to find help with you
I am getting an error that in this part:
`
value_result = []
value_skill = []
for i in range(df4.shape[0]):
job_description = df4['job_description'][i]
annotations = skill_extractor.annotate(job_description)
for type_matching, arr_skills in annotations["results"].items():
for skill in arr_skills:
if SKILL_DB[skill["skill_id"]]["skill_name"] in new_stopwords:
value_result.append(SKILL_DB[skill["skill_id"]]["skill_name"])
value_skill.append(SKILL_DB[skill["skill_id"]]["skill_type"])
`
and this is the error message
`
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3802 try:
-> 3803 return self._engine.get_loc(casted_key)
3804 except KeyError as err:
~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\anaconda3\lib\site-packages\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: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1268\3053340950.py in <module>
2 value_skill = []
3 for i in range(df4.shape[0]):
----> 4 job_description = df4['job_description'][i]
5 annotations = skill_extractor.annotate(job_description)
6 for type_matching, arr_skills in annotations["results"].items():
~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
979
980 elif key_is_scalar:
--> 981 return self._get_value(key)
982
983 if is_hashable(key):
~\anaconda3\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
1087
1088 # Similar to Index.get_value, but we do not fall back to positional
-> 1089 loc = self.index.get_loc(label)
1090 return self.index._get_values_for_loc(self, loc, label)
1091
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3803 return self._engine.get_loc(casted_key)
3804 except KeyError as err:
-> 3805 raise KeyError(key) from err
3806 except TypeError:
3807 # If we have a listlike key, _check_indexing_error will raise
KeyError: 0
`
i am try to solve but cant do this
Many thanks for your time, much appreciated.

I'm getting KeyError: 'bbox' in my code and I don't see why

This code is related to my research work. I am using the Yolo model for which I am creating bounding boxes. Now I am using this code for creating bounding boxes.
bboxs= np.stack(df['bbox'].apply(lambda x: np.formstring(x[1:-1], sep=',')))
for i, column in enumerate(['xmin','ymin','w','h'])
df[column]=bboxs[:,i]
df['x_center']=df['xmin'] + df['w']/2
df['x_center']=df['ymin'] + df['h']/2
df['classes']=0
df['w'] /=1024
df['h'] /=1024
df['x_center'] /=1024
df['y_center'] /=1024
df= df[['image_id','classes','x_center','y_center','w','h']]
df.head()
when I run this code got kind of error shows
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
4 frames
pandas/_libs/hashtable_class_helper.pxi inpandas._libs.hashtable.PyObjectHashTable.get_item()7
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'bbox'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 'bbox'
I am a beginner in python, so am unable to figure out what the issue is. I tried looking for solutions to similar questions but could not resolve the issue.
KeyError: 'bbox' when accessing the column of a dataframe (df) indicates that there is no column with the name 'bbox' in df.
To check which columns exist, they could be output as follows: print(df.columns).

Pandas df: retrieving the records that have cell value == float not working. What am I doing wrong?

I have this code and I can't figure out what to do to retrieve the row that I want.
I am trying to retrieve the row that has device_id=16384035. I tried both float and integer and also string there (since it tells me the column is object) but none worked
print(s_devices['Device ID'])
print(s_devices.columns)
print(s_devices.iloc[0,1])
print(type(s_devices.iloc[0,1]))
print(s_devices[['Device ID']==float(16384035)])
The above prints the below
0 16384035.0
1 17177488.0
2 16384036.0
3 17177489.0
4 0
...
534 17746358.0
535 17746359.0
536 17746360.0
537 17746361.0
538 17746362.0
Name: Device ID, Length: 539, dtype: object
Index(['Serial Number', 'Device ID', 'Device Name'.....some keys removed from here],
dtype='object')
16384035.0
<class 'float'>
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/.local/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~/.local/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
~/.local/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: False
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/tmp/ipykernel_540/2993650396.py in <module>
3 print(s_devices.iloc[0,1])
4 print(type(s_devices.iloc[0,1]))
----> 5 print(s_devices[['Device ID']==float(16384035)])
~/.local/lib/python3.9/site-packages/pandas/core/frame.py in __getitem__(self, key)
3453 if self.columns.nlevels > 1:
3454 return self._getitem_multilevel(key)
-> 3455 indexer = self.columns.get_loc(key)
3456 if is_integer(indexer):
3457 indexer = [indexer]
~/.local/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: False
You're indexing it wrong:
s_devices[['Device ID']==float(16384035)]
You create a boolean Series with the equality and index the DataFrame with it:
msk = s_devices['Device ID']==float(16384035)
out = s_devices[msk]
or as a one-liner:
out = s_devices[s_devices['Device ID']==float(16384035)]
Note that ['Device ID']==float(16384035) evaluates to False. So what you're trying to do is: s_devices[False] which raises the KeyError since there's no column named False.

Python code acts strange in apply function from groupby

Hello I have observed some quite strange pandas/python behavior and would like to understand how this comes to exist.
See the code below. When I call the code without line 2 it does not cause a problem (yes I do not do anything here) and print "hello" once per group. When I call it with this line it prints the correct result, then "hello" and then fails. According to the error message it fails in line 2 with Keyerror 0.
1 |def somefunction(group):
2 | # print(group["Date"][0]) # <- This line causes the problem...
3 | print("hello")
4 | return group
5 |
6 |
7 |groups= df_test.groupby(by=["UnitNumber", "Date"]).apply(somefunction)
How can this be? I am quite confused why he either fails and then still prints a message or that the program fails in the return because of a print.
In case anyone is fighting with the same problem I fixed it by using the following line instead of line 2.
print(group.iloc[0]["Date"].strftime('%Y-%m-%d'))
But I still would like to understand why the code above acts so strange. In case there are any know issues i am using Jupyterlabs.
EDIT:
As requested the error message (note that the line numbers are of course different):
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\scoop\apps\anaconda3\2021.05\lib\site-packages\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: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-266-55f0006d1f02> in <module>
----> 1 groups= df_test.groupby(by=["UnitNumber", "Date"]).apply(somefunction)
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, func, *args, **kwargs)
1251 with option_context("mode.chained_assignment", None):
1252 try:
-> 1253 result = self._python_apply_general(f, self._selected_obj)
1254 except TypeError:
1255 # gh-20949
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f, data)
1285 data after applying f
1286 """
-> 1287 keys, values, mutated = self.grouper.apply(f, data, self.axis)
1288
1289 return self._wrap_applied_output(
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
818 # group might be modified
819 group_axes = group.axes
--> 820 res = f(group)
821 if not _is_indexed_like(res, group_axes, axis):
822 mutated = True
<ipython-input-264-c99a31bdd0cf> in somefunction(group)
1 def somefunction(group):
----> 2 print(group["Date"][0]) # <- This line causes the problem...
3 # print("hello")
4 return group
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
940
941 elif key_is_scalar:
--> 942 return self._get_value(key)
943
944 if is_hashable(key):
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
1049
1050 # Similar to Index.get_value, but we do not fall back to positional
-> 1051 loc = self.index.get_loc(label)
1052 return self.index._get_values_for_loc(self, loc, label)
1053
~\scoop\apps\anaconda3\2021.05\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 0

KeyError while tring to write an additional field in Python Pandas dataframe

I want to add a calculated field 'Score' in dataframe positions_deposits.
When I run the following operation on pandas dataframe positions_deposits,
for i in range(len(positions_deposits)):
<Read some values from the dataframe which would be passed to a function in the next line>
Score = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
positions_deposits['Score'].loc[i] = Score
I get the following error. Can you please guide me through what error I am making and how to resolve it?
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2894 try:
-> 2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Score'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-201-7d0481b84aa4> in <module>
6 Values = positions_deposits['Values'].loc[i]
7 # Score = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
----> 8 positions_deposits["Score"].loc[i] = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
9
10 # print("Score is %i.00" %Score)
~/.local/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
2904 if self.columns.nlevels > 1:
2905 return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907 if is_integer(indexer):
2908 indexer = [indexer]
~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899 if tolerance is not None:
KeyError: 'Score'
Please note: if I print(Score), there is no error. It means the function, RAG_function is getting executed but the dataframe is failing.
Thanks!
You'll probably want to read up on how .loc and .iloc work. But having said that, there is another way which is better:
import pandas
import random
df = pandas.DataFrame([{"A": random.randint(0,100), "B": random.randint(0,100)} for _ in range(100)])
def rag_function(row):
A = row["A"]
B = row["B"]
return A * B
df["Score"] = df.apply(rag_function, axis=1)
NOTE: I don't have your RAG_function so I've created some random function. The idea is that you apply this function to every row in the dataframe.

Categories

Resources