2

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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.