I have the following code:
JavaScript
x
4
1
A = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=[['att1', 'att2']])
2
A['idx'] = ['a', 'b', 'c']
3
A
4
which works fine until I do (trying to set column ‘idx’ as in index for the dataframe)
JavaScript
1
2
1
A.set_index('idx', inplace=True)
2
which throws an error
JavaScript
1
2
1
TypeError: only integer scalar arrays can be converted to a scalar index
2
What does this mean ?
Advertisement
Answer
The error is when you create A
with
JavaScript
1
2
1
columns = [['att1', 'att2']]
2
If you print A.columns
you will get:
JavaScript
1
5
1
MultiIndex([('att1',),
2
('att2',),
3
( 'idx',)],
4
)
5
So 'idx'
is not really in your column for you to set index. Now, this would work:
JavaScript
1
2
1
A.set_index(('idx',))
2
and give:
JavaScript
1
6
1
att1 att2
2
(idx,)
3
a 1 2
4
b 1 3
5
c 4 6
6
However, you should fix your creation of A
with just:
JavaScript
1
2
1
columns = ['att1', 'att2']
2