Python Forum
strange error from pandas dataframe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: strange error from pandas dataframe (/thread-28620.html)



strange error from pandas dataframe - djf123 - Jul-26-2020

I am trying to apply the command
example_batch = normed_train_data[:10]
to the pandas data frame named normed_train_data, but I am getting an error message I don't know how to fix or why its occurring. Below is the error message.
Error:
Traceback (most recent call last): File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4845, in get_slice_bound return self._searchsorted_monotonic(label, side) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4806, in _searchsorted_monotonic raise ValueError("index must be monotonic increasing or decreasing") ValueError: index must be monotonic increasing or decreasing During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\crovn\Desktop\RegressionSound2.py", line 101, in <module> example_batch = normed_train_data[:10] File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\frame.py", line 2779, in __getitem__ indexer = convert_to_index_sliceable(self, key) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexing.py", line 2267, in convert_to_index_sliceable return idx._convert_slice_indexer(key, kind="getitem") File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\numeric.py", line 424, in _convert_slice_indexer return self.slice_indexer(key.start, key.stop, key.step, kind=kind) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4713, in slice_indexer start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4932, in slice_locs end_slice = self.get_slice_bound(end, "right", kind) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4848, in get_slice_bound raise err File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 4842, in get_slice_bound slc = self.get_loc(label) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\numeric.py", line 508, in get_loc return super().get_loc(key, method=method, tolerance=tolerance) File "C:\Users\crovn\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 133, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine._get_loc_duplicates File "pandas\_libs\index_class_helper.pxi", line 54, in pandas._libs.index.Float64Engine._maybe_get_bool_indexer KeyError: 10
I converted the normed_train_data to a csv so you can see its contents. The file is named dataframe1.csv and is attached to this thread. I tried the command
example_batch = normed_train_data[:10]
on a different data frame attached as dataframe2.csv and for some reason the command works on that one, but not on the dataframe1.csv one.

Why am I getting this error message, and How do I make this command work?


RE: strange error from pandas dataframe - scidam - Jul-27-2020

If you want to get the first 10 rows of the dataframe, you need to use iloc, e.g.
normed_train_data.iloc[:10]