0

Following script:

import pandas as pd
import numpy as np
import math

A = pd.DataFrame(np.array([[1,2,3,4],[5,6,7,8]]))

Floor1 = math.floor(A.min()[1]/2)*2

names = np.array([ 0.   ,  0.635,  1.27 ,  1.905])

A.columns = names

Floor2 = math.floor(A.min()[1]/2)*2

Floor1 is being executed correctly, Floor2 which is done with the same df but with renamed columns isn't. I get a key error:

Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)

  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 392, in pandas._libs.hashtable.Float64HashTable.get_item

KeyError: 1.0


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\Desktop\Python\untitled0.py", line 13, in <module>
    Floor2 = math.floor(A.min()[1]/2)*2

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\numeric.py", line 449, in get_value
    loc = self.get_loc(k)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\numeric.py", line 508, in get_loc
    return super().get_loc(key, method=method, tolerance=tolerance)

  File "C:\ProgramData\Anaconda3\lib\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 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 392, in pandas._libs.hashtable.Float64HashTable.get_item

KeyError: 1.0

I know, there is a similar question: After rename column get keyerror

But I didn't really get the answer and - more important - how to solve it.

masterofpuppets
  • 119
  • 2
  • 11

2 Answers2

3

Before renaming if you get the columns of A using list(A.columns), you'll see that you'll get the list [0,1,2,3]. So, you can index using the key 1. However, after renaming, you can no longer index with key 1 because the column names have changed.

1

If you are using A.min(), you are finding minimum value in axis=0 by default that is along columns.
When changing the column names, you cannot access index '1' as there is no index with the name '1' in the columns.

If Your intension is finding the minimum in a row, you can use A.min(axis=1).

You can write the code like this.

import pandas as pd
import numpy as np
import math

A = pd.DataFrame(np.array([[1,2,3,4],[5,6,7,8]]))

Floor1 = math.floor(A.min(axis=1)[1]/2)*2

names = np.array([ 0.   ,  0.635,  1.27 ,  1.905])

A.columns = names

Floor2 = math.floor(A.min(axis=1)[1]/2)*2

Thank you

Dinesh
  • 812
  • 4
  • 14