Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefining the Index in a Pandas DataFrame object

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?

like image 299
nitin Avatar asked May 05 '12 00:05

nitin


People also ask

How do I change the index of a pandas DataFrame?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

How do I change the index of an existing DataFrame in Python?

To set the DataFrame index using existing columns or arrays in Pandas, use the set_index() method. The set_index() function sets the DataFrame index using existing columns. The index can replace the existing index or expand on it.

How do you reassign an index in Python?

If you want to keep the original index as a column, use reset_index() to reassign the index to a sequential number starting from 0 . You can change the index to a different column by using set_index() after reset_index() .

How do I reindex in pandas?

One can reindex a single column or multiple columns by using reindex() method and by specifying the axis we want to reindex. Default values in the new index that are not present in the dataframe are assigned NaN.


2 Answers

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 
like image 22
Aakash Saxena Avatar answered Oct 01 '22 10:10

Aakash Saxena


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 
like image 168
Avaris Avatar answered Oct 01 '22 10:10

Avaris