QuantyZxxxx
QuantyZxxxx

Reputation: 65

Type error: only integer scalar arrays can be converted to a scalar index when doing .loc with pandas DataFrame

I have a simple df:

a = pd.DataFrame([[1,2,3,5,8],['jack','jeff',np.nan,np.nan,'tesla']])
    a.index = [['number','name']]
    a=a.T

and it looks like this:

  number   name
0      1   jack
1      2   jeff
2      3    NaN
3      5    NaN
4      8  tesla

When I am tring to do a .loc like a.loc[a['number']==5], I got this type error:

Traceback (most recent call last):
  File "c:\Users\Administrator\Documents\proj\test.py", line 13, in <module>
    a.loc[a['number']==5]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2878, in __getitem__
    return self._get_item_cache(key)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3541, in _get_item_cache
    values = self._mgr.iget(loc)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 988, in iget
    block = self.blocks[self.blknos[i]]
TypeError: only integer scalar arrays can be converted to a scalar index

I searched this error and tried some solutions like using a.loc[np.array(a)['number']==5] or reinstall pandas and numpy or anaconda but they are not working.

My pandas version is 1.3 and numpy version is 1.19.2

Upvotes: 1

Views: 817

Answers (1)

akuiper
akuiper

Reputation: 215047

The reason being that your column is MultiIndex:

a.columns
#MultiIndex([('number',),
#            (  'name',)],
#           )

The error occurs when you do a['number']. Replacing the index rename with a list instead of list of lists should fix, i.e. instead of:

a.index = [['number','name']]

Do:

a.index = ['number','name']

Upvotes: 1

Related Questions