简体   繁体   中英

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]

Try

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


df = df.reindex(new_inex)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM