How To Change Pandas Dataframe Index Value?
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
Solution 1:
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
Post a Comment for "How To Change Pandas Dataframe Index Value?"