2016-12-29 35 views
0

我有一個系列,如如下:大熊貓重新編制了一系列

indic={'indicator_name': {0: '10-Year Note Auction', 
         1: '10-Year TIPS Auction', 
         2: '2-Year Note Auction', 
         3: '3-Month Bill Auction', 
         4: '3-Year Note Auction'}} 

ind_serie=pd.Series(indic) 

我想重新索引它,以便值也是指數(即:0: '10-Year Note Auction'成爲'10-Year Note Auction': '10-Year Note Auction'等)

如果ind_serie是一個數據幀我會用:

ind_serie.set_index('indicator_name', drop=False, inplace=True) 

重新索引功能也似乎沒有工作。

ind_serie.reindex(index='indicator_name') 

但是,在系列的情況下使用正確的語法是什麼?

+0

我想你的意思是系列的方式。 – 2016-12-29 02:57:27

+0

@fuzzyhedge:是... –

回答

1

重新分配指標

ind_serie.index = ind_serie.values 
+0

@ piRSquared Thx!然而,我想知道爲什麼有時候索引出現正確和正如預期的那樣,有時甚至是'(10年票據拍賣)'(即括號和逗號?) –

+0

@jimbasquiat,這是因爲這就是系列值。 – piRSquared

1

你有類型的字典字典創建包含第一字典的列表中的一個元素的系列。這是一個不同的問題。

下面我創建了你想要的項目列表。

s1 = pd.Series(
     ['10-Year TIPS Auction', 
     '2-Year Note Auction', 
     '3-Month Bill Auction'], name='s1') 

s2 = s1.copy(deep=True).rename('s2') 
s3 = pd.DataFrame(s1.values, index=s2, columns=['s1']) 


s3 
             s1 
s2           
10-Year TIPS Auction 10-Year TIPS Auction 
2-Year Note Auction 2-Year Note Auction 
3-Month Bill Auction 3-Month Bill Auction 

另一種方式......

s3 = pd.DataFrame(s1, columns=['s1']) 
s3.index = s1.values 
s3 
             s1 
10-Year TIPS Auction 10-Year TIPS Auction 
2-Year Note Auction 2-Year Note Auction 
3-Month Bill Auction 3-Month Bill Auction 
+0

@ fuzzyhedge Thx!然而,我想知道爲什麼有時索引出現正確和預期,有時作爲'(10年票據拍賣)「(即括號和逗號?) –

+0

這可能意味着它被視爲它自己的形狀(1)。在導入過程中可能在它周圍有[]附加[]。類似的(和相關的)問題作爲一個元素被輸入到系列中的字典。 – 2016-12-29 04:42:05

+0

我建議通過[split-apply-combine](http://pandas.pydata.org/pandas-docs/stable/groupby.html)文檔。 – 2016-12-29 04:47:32

0

ind_serie.index = ind_serie.values沒有正常工作

ind_serie.to_frame.set_index('indicator_name', drop=False, inplace=True) 是去最終