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
  update values in one dataframe based on another dataframe - Pandas iliasb 2 1,474 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 782 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,096 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 881 Dec-17-2020, 01:08 PM
Last Post: palladium
  How to search for specific string in Pandas dataframe Coding_Jam 1 1,131 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 1,903 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 916 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  Pandas DataFrame not updating HelpMePlease 3 1,199 Jul-11-2020, 07:19 PM
Last Post: jefsummers
  Pandas DataFrame visual Truman 8 1,716 Jul-10-2020, 06:11 AM
Last Post: hussainmujtaba
  Pandas DataFrame and unmatched column sritsv19 0 1,553 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

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