The index that I have in the dataframe (with 30 rows) is of the form:
Int64Index([171, 174,173, 172, 199..............
....175, 200])
The index is not strictly increasing because the data frame is the output of a sort(). I want to have add a column which is the series:
[1, 2, 3, 4, 5......................., 30]
How should I go about doing that?
Thanks.
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.
Source: Stackoverflow.com