Reputation: 7423
I am trying to get confident with Pandas, I would like to understand how can I use one generic DataFrame column as row index and how can I delete it from the matrix.
Say that I have a matrix such as
a b c d e
11 2 1 0 aa 2
22 1 1 0 bb 1
33 4 b 3 cc 9
44 5 2 2 dd 5
55 2 9 8 ee 6
in which the first column and the first row are not data but indexes. I would like the d column ('aa', 'bb', 'cc', 'dd', 'ee') to be the row index, I don't care of the original row index and I don't want the 'd' column to be a matrix column. Long story short I would like a matrix such as
a b c e
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
in which 'a', 'b', 'c', 'e' and 'aa', 'bb', 'cc', 'dd', 'ee' are column and row indexes respectively. How can I do this job?
Upvotes: 2
Views: 2872
Reputation: 862441
You can use set_index
:
print df
a b c d e
11 2 1 0 aa 2
22 1 1 0 bb 1
33 4 b 3 cc 9
44 5 2 2 dd 5
55 2 9 8 ee 6
print df.set_index('d')
a b c e
d
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
Or with reset index name
:
df = df.set_index('d')
df.index.name= None
print df
a b c e
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
Upvotes: 4