Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert index of a pandas dataframe into a column

This seems rather obvious, but I can't seem to figure out how to convert an index of data frame to a column?

For example:

df=         gi       ptt_loc  0  384444683      593    1  384444684      594   2  384444686      596   

To,

df=     index1    gi       ptt_loc  0  0     384444683      593    1  1     384444684      594   2  2     384444686      596   
like image 634
msakya Avatar asked Dec 09 '13 00:12

msakya


People also ask

How do I turn a DataFrame index into a column?

Convert the Index to Column Another way is by using the pandas. Dataframe. reset_index() function to convert the index as a column.

How do I convert multiple index to columns in pandas?

pandas MultiIndex to ColumnsUse pandas DataFrame. reset_index() function to convert/transfer MultiIndex (multi-level index) indexes to columns. The default setting for the parameter is drop=False which will keep the index values as columns and set the new index to DataFrame starting from zero.

How do you convert the index of a DataFrame to a list in Python?

tolist() function return a list of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period). Example #1: Use Index. tolist() function to convert the index into a list.

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.


2 Answers

either:

df['index1'] = df.index 

or, .reset_index:

df.reset_index(level=0, inplace=True) 

so, if you have a multi-index frame with 3 levels of index, like:

>>> df                        val tick       tag obs         2016-02-26 C   2    0.0139 2016-02-27 A   2    0.5577 2016-02-28 C   6    0.0303 

and you want to convert the 1st (tick) and 3rd (obs) levels in the index into columns, you would do:

>>> df.reset_index(level=['tick', 'obs'])           tick  obs     val tag                         C   2016-02-26    2  0.0139 A   2016-02-27    2  0.5577 C   2016-02-28    6  0.0303 
like image 64
behzad.nouri Avatar answered Sep 30 '22 21:09

behzad.nouri


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')) 

enter image description here

The reset_index method, called with the default parameters, converts all index levels to columns and uses a simple RangeIndex as new index.

df.reset_index() 

enter image description here

Use the 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) 

enter image description here

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 level df.assign(State=df.index.get_level_values('State'))  # for all levels df.assign(**df.index.to_frame()) 
like image 26
Ted Petrou Avatar answered Sep 30 '22 21:09

Ted Petrou