I get an error when trying to access a single element in a pandas dataframe this way test_df["LABEL"][0]. Here is a code snippet on how I am loading the data:
print "reading test set"
test_set = pd.read_csv(data_path+"small_test_products.txt", header=0, delimiter="|")
print "shape of the test set", test_set.shape
test_df = pd.DataFrame(test_set)
lengthOfTestSet = len(test_df["LABEL"])
print test_df["LABEL"][0]
Here is the error I am getting:
File "code.py", line 80, in print test_df["LABEL"][0] File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 521, in getitem result = self.index.get_value(self, key) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 3562, in get_value loc = self.get_loc(k) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 3619, in get_loc return super(Float64Index, self).get_loc(key, method=method) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 1572, in get_loc return self._engine.get_loc(_values_from_object(key)) File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824) File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3704) File "pandas/hashtable.pyx", line 541, in pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9914)
File "pandas/hashtable.pyx", line 547, in pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9852) KeyError: 0.0
What am I missing?
This error occurs when you attempt to access some column in a pandas DataFrame that does not exist. Typically this error occurs when you simply misspell a column names or include an accidental space before or after the column name. The following example shows how to fix this error in practice.
In Python, we can set any Python object like a list, range, or series as the index of the Pandas DataFrame object in the following ways. 1. Python list as the index of the DataFrame In this method, we can set the index of the Pandas DataFrame object using the pd.Index (), range (), and set_index () function.
In Python, we can easily set any existing column or columns of a Pandas DataFrame object as its index in the following ways. 1. Set column as the index (without keeping the column) In this method, we will make use of the inplace parameter which is an optional parameter of the set_index () function of the Python Pandas module.
pandas provides a suite of methods in order to have purely label based indexing. This is a strict inclusion based protocol. Every label asked for must be in the index, or a KeyError will be raised. When slicing, both the start bound AND the stop bound are included, if present in the index.
Like EdChum said 0 is probably not in your index.
Try: df.iloc[0]
or df['label'].iloc[0]
, which is integer based location.
To reset the index if you are having trouble with that: df.reset_index(drop=True)
Check out panda's indexing doc for more information on it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With