2017-08-08 81 views
0

我有一個按時間排序的日期時間系列(注意在左側的索引值)更換大熊貓用自己的指數系列列值

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

對於我的目的,只排序的索引很重要,所以我想用它們在原熊貓系列中的索引替換日期時間值(可能通過重新索引)以返回如下新系列:

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

其中的左手側的「指數」是新的「索引」列,並在右側的「指數」是datetime值原來的索引列。

有什麼更簡單的方法來做到這一點?

謝謝。

+1

's = pd.Series(s.index,name = s。 index.name)'哪裏s'是原始系列。 – Alexander

+0

這工作,謝謝! –

回答

1

如果你是好與構建新的對象:

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

如果指定新的指數是可選..

1

如下

df.index = list(range(len(df)) 
你可以點你的索引列表

其中df是你的數據幀