How to convert index of a pandas dataframe into a column
df['index1'] = df.index
so, if you have a multi-index frame with 3 levels of index, like:
2016-02-26 C 2 0.01392016-02-27 A 2 0.55772016-02-28 C 6 0.0303df valtick tag obs
and you want to convert the 1st (
tick) and 3rd (
obs) levels in the index into columns, you would do:
'tick', 'obs']) tick obs valtag C 2016-02-26 2 0.0139A 2016-02-27 2 0.5577C 2016-02-28 6 0.0303df.reset_index(level=[
To provide a bit more clarity, let's look at a DataFrame with two levels in its index (a MultiIndex).
index = pd.MultiIndex.from_product([['TX', 'FL', 'CA'], ['North', 'South']], names=['State', 'Direction'])df = pd.DataFrame(index=index, data=np.random.randint(0, 10, (6,4)), columns=list('abcd'))
reset_index method, called with the default parameters, converts all index levels to columns and uses a simple
RangeIndex as new index.
level parameter to control which index levels are converted into columns. If possible, use the level name, which is more explicit. If there are no level names, you can refer to each level by its integer location, which begin at 0 from the outside. You can use a scalar value here or a list of all the indexes you would like to reset.
df.reset_index(level='State') # same as df.reset_index(level=0)
In the rare event that you want to preserve the index and turn the index into a column, you can do the following:
# for a single leveldf.assign(State=df.index.get_level_values('State'))# for all levelsdf.assign(**df.index.to_frame())