Skip to content
Advertisement

Error when trying to set column as index in pandas dataframe

I have the following code:

A = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=[['att1', 'att2']])
A['idx'] = ['a', 'b', 'c']
A

which works fine until I do (trying to set column ‘idx’ as in index for the dataframe)

A.set_index('idx', inplace=True)

which throws an error

TypeError: only integer scalar arrays can be converted to a scalar index

What does this mean ?

Advertisement

Answer

The error is when you create A with

columns = [['att1', 'att2']]

If you print A.columns you will get:

MultiIndex([('att1',),
            ('att2',),
            ( 'idx',)],
           )

So 'idx' is not really in your column for you to set index. Now, this would work:

A.set_index(('idx',))

and give:

       att1 att2
(idx,)          
a         1    2
b         1    3
c         4    6

However, you should fix your creation of A with just:

columns = ['att1', 'att2']
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement