user2644687
user2644687

Reputation: 51

Python: assign a pandas data frame a new index

I have a pandas data frame with 41 rows. It has an index series. How may I rename the index as -20, -19, -18, ...-1, 0, 1, ... 20? Thanks!!

df
Out[102]: 
                   ABC         DEF  ...         TUV         WXY
2011-01-03         NaN         NaN  ...         NaN         NaN
2011-01-04         NaN         NaN  ...         NaN         NaN
2011-01-05         NaN         NaN  ...         NaN         NaN
2011-01-06         NaN         NaN  ...         NaN         NaN
2011-01-07         NaN         NaN  ...         NaN         NaN
......
2011-02-22         NaN         NaN  ...         NaN         NaN
2011-02-23         NaN         NaN  ...         NaN         NaN
2011-02-24         NaN         NaN  ...         NaN         NaN
2011-02-25         NaN         NaN  ...         NaN         NaN
2011-02-28         NaN         NaN  ...         NaN         NaN

[41 rows x 15 columns]

Upvotes: 0

Views: 31

Answers (1)

pi_pascal
pi_pascal

Reputation: 202

Try

new_inex = list(range(-20,21))


df = df.reindex(new_inex)

Upvotes: 1

Related Questions