1

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 ?

Very Confused
  • 35
  • 1
  • 5
  • 1
    Duplicate https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d – forgetso Oct 23 '20 at 14:13

1 Answers1

3

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']
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74