Stacey
Stacey

Reputation: 5097

Changing a dataframe index to integer

I have a dataframe called rollTestLogReturnsData which is indexed by date

                      BBG.KABN.S      BBG.TKA.S       BBG.CON.S        BBG.ISAT.S  
   date
   2015-02-17        -0.005821       -0.016792       -0.016111         0.001028   
   2015-02-18         0.000588        0.019169       -0.000307        -0.001832   
   2015-02-19         0.005041       -0.007580        0.004164         0.003923   
   2015-02-20        -0.004881        0.008011        0.007047        -0.000307   
   2015-02-23         0.007468       -0.011277       -0.003273         0.004355

I then create another dataframe called rollReturnRandomDf indexed by date which is a random selection of rows from rollTestLogReturnsData. This is constructed using:

rollReturnRandomDf = rollTestLogReturnsData.ix[np.random.choice(rollTestLogReturnsData.index, len(rollTestLogReturnsData.index))]

which can look like:

                      BBG.KABN.S     BBG.TKA.S      BBG.CON.S         BBG.ISAT.S  
  date                                                                           
  2015-02-20        -0.004881        0.008011        0.007047        -0.000307   
  2015-02-20        -0.004881        0.008011        0.007047        -0.000307   
  2015-02-19         0.005041       -0.007580        0.004164         0.003923    
  2015-02-17        -0.005821       -0.016792       -0.016111         0.001028   
  2015-02-20        -0.004881        0.008011        0.007047        -0.000307  

Please note that the index date can be the same for multiple rows (in this case 2015-02-20 occurs 3 times). The problems start when I interrogate rollTestLogReturnsData.

Is there a way to reindex the rollTestLogReturnsData dataframe with integers so it would look like:

                     BBG.KABN.S     BBG.TKA.S      BBG.CON.S         BBG.ISAT.S  
  Index                                                                           
  0                  -0.004881        0.008011        0.007047        -0.000307   
  1                  -0.004881        0.008011        0.007047        -0.000307   
  2                   0.005041       -0.007580        0.004164         0.003923    
  3                  -0.005821       -0.016792       -0.016111         0.001028   
  4                  -0.004881        0.008011        0.007047        -0.000307

I have tried manipulating the above line of code but have failed to come up with a way to do this. What can I try next?

Upvotes: 0

Views: 241

Answers (2)

learn2day
learn2day

Reputation: 1716

You should use the .set_index method for the dataFrame.

newIndex = [i for i in range(len(rollTestLogReturnsData))]
newDF = rollTestLogReturnsData.set_index(newIndex)

Upvotes: 0

Jianxun Li
Jianxun Li

Reputation: 24742

try rollReturnRandomDf.reset_index(drop=True)

Before:

Out[170]: 
                 A       B       C       D
0                                         
2015-02-19  0.0050 -0.0076  0.0042  0.0039
2015-02-18  0.0006  0.0192 -0.0003 -0.0018
2015-02-19  0.0050 -0.0076  0.0042  0.0039
2015-02-17 -0.0058 -0.0168 -0.0161  0.0010
2015-02-18  0.0006  0.0192 -0.0003 -0.0018

After:

Out[171]: 
        A       B       C       D
0  0.0050 -0.0076  0.0042  0.0039
1  0.0006  0.0192 -0.0003 -0.0018
2  0.0050 -0.0076  0.0042  0.0039
3 -0.0058 -0.0168 -0.0161  0.0010
4  0.0006  0.0192 -0.0003 -0.0018

Upvotes: 1

Related Questions