Chris T.
Chris T.

Reputation: 1801

Replacing pandas Series column values with their own indices

I have a chronologically-sorted datetime Series(note the index values on the left-hand side)

9     1971-04-10
84    1971-05-18
2     1971-07-08
53    1971-07-11
28    1971-09-12
474   1972-01-01
153   1972-01-13
13    1972-01-26
129   1972-05-06
98    1972-05-13
111   1972-06-10
225   1972-06-15

For my purpose, only the sorted indices matter, so I would like to replace the datetime values with their indices in the original pandas Series (perhaps through reindexing) to return a new Series like this:

0   9
1   84 
2   2  
3   53   
4   28    
5   474  
6   153  
7   13   
8   129 
9   98   
10  111  
11  225

where the 'indices' on the left-hand-side are the new 'index' column and the 'indices' on the right are the original index column for datetime values.

What is the easier way to do this?

Thank you.

Upvotes: 0

Views: 329

Answers (2)

Raja Sattiraju
Raja Sattiraju

Reputation: 1272

You can point your index to a list as follows

df.index = list(range(len(df))

where df is your dataframe

Upvotes: 1

Uvar
Uvar

Reputation: 3462

If you are okay with constructing a new object:

series = pd.Series(old_series.index, index=whateveryouwant)

Where specifying the new index is optional..

Upvotes: 1

Related Questions