Python Forum
strange error from pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strange error from pandas dataframe
#1
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?

Attached Files

.csv   dataframe1.csv (Size: 28.69 KB / Downloads: 10)
.csv   dataframe2.csv (Size: 55.37 KB / Downloads: 9)
Reply
#2
If you want to get the first 10 rows of the dataframe, you need to use iloc, e.g.
normed_train_data.iloc[:10]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pip install pandas ERROR pythondudu 7 19,915 May-24-2022, 06:14 AM
Last Post: Marya_475
  Pandas Dataframe Filtering based on rows mvdlm 0 607 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 1,190 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 559 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 784 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 622 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 4,878 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 1,501 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,830 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 1,524 Dec-17-2020, 01:08 PM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020