Pandas (python): How to add column to dataframe for index?

How about:

df['new_col'] = range(1, len(df) + 1)

Alternatively if you want the index to be the ranks and store the original index as a column:

df = df.reset_index()

I stumbled on this question while trying to do the same thing (I think). Here is how I did it:

df['index_col'] = df.index

You can then sort on the new index column, if you like.


How about this:

from pandas import *

idx = Int64Index([171, 174, 173])
df = DataFrame(index = idx, data =([1,2,3]))
print df

It gives me:

     0
171  1
174  2
173  3

Is this what you are looking for?


The way to do that would be this:

Resetting the index:

df.reset_index(drop=True, inplace=True)

Sorting an index:

df.sort_index(inplace=True)

Setting a new index from a column:

df.set_index('column_name', inplace=True)

Setting a new index from a range:

df.index = range(1, 31, 1) #a range starting at one ending at 30 with a stepsize of 1.

Sorting a dataframe based on column value:

df.sort_values(by='column_name', inplace=True)

Reassigning variables works as-well:

df=df.reset_index(drop=True)
df=df.sort_index()
df=df.set_index('column_name')
df.index = range(1, 31, 1) #a range starting at one ending at 30 with a stepsize of 1.
df=df.sort_values(by='column_name')