Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Pandas dataframe index value?

Tags:

python

pandas

I have a df:

>>> df
                   sales     cash
STK_ID RPT_Date                  
000568 20120930   80.093   57.488
000596 20120930   32.585   26.177
000799 20120930   14.784    8.157

And want to change first row's index value from ('000568','20120930') to ('000999','20121231'). Final result will be:

>>> df
                   sales     cash
STK_ID RPT_Date                  
000999 20121231   80.093   57.488
000596 20120930   32.585   26.177
000799 20120930   14.784    8.157

How to achieve this?

like image 707
bigbug Avatar asked Jan 01 '13 13:01

bigbug


People also ask

How do I change the index of a DataFrame in pandas?

To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.

How do I change the index of a DataFrame label?

You can rename (change) column/index names of pandas. DataFrame by using rename() , add_prefix() , add_suffix() , set_axis() methods or updating the columns / index attributes. You can also rename index names (labels) of pandas. Series in the same way.

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 fix index in pandas?

Use DataFrame.reset_index() function We can use DataFrame. reset_index() to reset the index of the updated DataFrame. By default, it adds the current row index as a new column called 'index' in DataFrame, and it will create a new row index as a range of numbers starting at 0.


1 Answers

With this setup:

import pandas as pd
import io

text = '''\
STK_ID RPT_Date sales cash
000568 20120930 80.093 57.488
000596 20120930 32.585 26.177
000799 20120930 14.784 8.157
'''

df = pd.read_csv(io.BytesIO(text), delimiter = ' ', 
                 converters = {0:str})
df.set_index(['STK_ID','RPT_Date'], inplace = True)

The index, df.index can be reassigned to a new MultiIndex like this:

index = df.index
names = index.names
index = [('000999','20121231')] + df.index.tolist()[1:]
df.index = pd.MultiIndex.from_tuples(index, names = names)
print(df)
#                   sales    cash
# STK_ID RPT_Date                
# 000999 20121231  80.093  57.488
# 000596 20120930  32.585  26.177
# 000799 20120930  14.784   8.157

Or, the index could be made into columns, the values in the columns could be then reassigned, and then the columns returned to indices:

df.reset_index(inplace = True)
df.ix[0, ['STK_ID', 'RPT_Date']] = ('000999','20121231')
df = df.set_index(['STK_ID','RPT_Date'])
print(df)

#                   sales    cash
# STK_ID RPT_Date                
# 000999 20121231  80.093  57.488
# 000596 20120930  32.585  26.177
# 000799 20120930  14.784   8.157

Benchmarking with IPython %timeit suggests reassigning the index (the first method, above) is significantly faster than resetting the index, modifying column values, and then setting the index again (the second method, above):

In [2]: %timeit reassign_index(df)
10000 loops, best of 3: 158 us per loop

In [3]: %timeit reassign_columns(df)
1000 loops, best of 3: 843 us per loop
like image 51
unutbu Avatar answered Sep 21 '22 17:09

unutbu