2017-07-02 37 views
1

我用的是熊貓版「0.20.1」,蟒蛇3ValueError異常:索引必須是單調遞增或遞減,其中包括索引,列ffill同時

雖然有兩個問題: question1question2 已被問及同樣的錯誤,而我發現這兩個問題與我的情況不一樣。

該數據來自「用於數據分析的Python」一書,第123-124頁。 當我運行下面的代碼,

frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'], 
        columns=['Ohio', 'Texas', 'California']) 
states = ['Texas', 'Utah', 'California'] 
frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill',columns=states) 

它報告錯誤

ValueError: index must be monotonic increasing or decreasing 

,而我曾嘗試以下兩種表現,他們成功地運行:

frame.reindex(index=['a', 'b', 'c', 'd'], columns=states) 

frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill') 

***********************更新***************

我試過這段代碼,

frame3=frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill').reindex(columns=states) 

然後它返回與本書相同的結果。

Out[92]: 
Texas Utah California 
a 1 NaN 2 
b 1 NaN 2 
c 4 NaN 5 
d 7 NaN 8 
+0

你使用的是什麼版本的熊貓?你的確切代碼適用於我0.18.1 – DeepSpace

+0

pandas版本是'0.20.1',python 3 – Renke

+0

@ DeepSpace,你的意思是「frame3 = frame.reindex(index = ['a','b','c' ,'d'],method ='ffill')。reindex(columns = states)「適合你? – Renke

回答

0

因爲列也是重新編制索引,而不是單調遞增或遞減。

+0

考慮添加更多的細節/上下文來幫助OP –