87

I am trying to re-index a pandas DataFrame object, like so,

From:
            a   b   c
        0   1   2   3
        1  10  11  12
        2  20  21  22

To :
           b   c
       1   2   3
      10  11  12
      20  21  22

I am going about this as shown below and am getting the wrong answer. Any clues on how to do this?

>>> col = ['a','b','c']
>>> data = DataFrame([[1,2,3],[10,11,12],[20,21,22]],columns=col)
>>> data
    a   b   c
0   1   2   3
1  10  11  12
2  20  21  22
>>> idx2 = data.a.values
>>> idx2
array([ 1, 10, 20], dtype=int64)
>>> data2 = DataFrame(data,index=idx2,columns=col[1:])
>>> data2
     b   c
1   11  12
10 NaN NaN
20 NaN NaN

Any idea why this is happening?

1
  • 1
    because you are using the 1st DF to build the second, this will only get the rows where idx2 intersect the data.index, ie. row 1 Commented Jun 27, 2012 at 12:56

3 Answers 3

192

Why don't you simply use set_index method?

In : col = ['a','b','c']

In : data = DataFrame([[1,2,3],[10,11,12],[20,21,22]],columns=col)

In : data
Out:
    a   b   c
0   1   2   3
1  10  11  12
2  20  21  22

In : data2 = data.set_index('a')

In : data2
Out:
     b   c
a
1    2   3
10  11  12
20  21  22
Sign up to request clarification or add additional context in comments.

1 Comment

In order to remove the index name, as in the original example: data2.index.name = None
5

If you don't want 'a' in the index

In :

col = ['a','b','c']

data = DataFrame([[1,2,3],[10,11,12],[20,21,22]],columns=col)

data

Out:

    a   b   c
0   1   2   3
1  10  11  12
2  20  21  22

In :

data2 = data.set_index('a')

Out:

     b   c
a
1    2   3
10  11  12
20  21  22

In :

data2.index.name = None

Out:

     b   c
 1   2   3
10  11  12
20  21  22

1 Comment

The second Out is not correct. There is still an unnamed column on the left containing 0, 1, 2.
0

To avoid index name with a single-liner, you can use set_index('a') with rename_axis(None)

In [8]: data.set_index('a').rename_axis(None)
Out[8]: 
     b   c
1    2   3
10  11  12
20  21  22

Details

In [9]: data
Out[9]: 
    a   b   c
0   1   2   3
1  10  11  12
2  20  21  22

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.