2017-08-12 188 views
0

我試圖下面的一行指數從大到小排序:遇到問題通過行索引和`sort_index排序()`不工作

enter image description here

我第一次嘗試是:

plot_df_dropoff.sort_index(by=["dropoff_latitude"], ascending=False) 

但我得到了一個Key Value Error

基於this link的第二個想法也沒有工作。它返回None

這似乎很簡單,但我無法弄清楚。任何幫助將非常感激。

id 
pickup_longitude (-74.03, -74.025] (-74.025, -74.02] (-74.02, -74.015] (-74.015, -74.01] (-74.01, -74.005] (-74.005, -74] (-74, -73.995] (-73.995, -73.99] (-73.99, -73.985] (-73.985, -73.98] ... (-73.82, -73.815] (-73.815, -73.81] (-73.81, -73.805] (-73.805, -73.8] (-73.8, -73.795] (-73.795, -73.79] (-73.79, -73.785] (-73.785, -73.78] (-73.78, -73.775] (-73.775, -73.77] 
pickup_latitude                     
(40.63, 40.64] 5.0 10.0 8.0 2.0 3.0 1.0 NaN 2.0 1.0 1.0 ... NaN NaN NaN NaN 1.0 NaN 7.0 1.0 NaN NaN 
(40.64, 40.65] 2.0 2.0 14.0 16.0 2.0 4.0 6.0 3.0 5.0 11.0 ... NaN NaN NaN 149.0 164.0 3580.0 7532.0 11381.0 5596.0 NaN 
(40.65, 40.66] NaN NaN NaN 2.0 22.0 41.0 11.0 2.0 4.0 13.0 ... NaN 1.0 146.0 7.0 3.0 201.0 81.0 2.0 1.0 2.0 
(40.66, 40.67] NaN NaN NaN NaN NaN 2.0 60.0 143.0 180.0 122.0 ... NaN 4.0 24.0 126.0 15.0 47.0 32.0 4.0 3.0 3.0 
(40.67, 40.68] NaN NaN 7.0 44.0 18.0 200.0 328.0 65.0 293.0 590.0 ... 3.0 3.0 1.0 131.0 1.0 1.0 2.0 1.0 1.0 2.0 

這裏是一個小片段,可能是更容易的工作有:

       id          \ 
pickup_longitude (-74.03, -74.025] (-74.025, -74.02] (-74.02, -74.015] 
pickup_latitude               
(40.63, 40.64]     5.0    10.0    8.0 
(40.64, 40.65]     2.0    2.0    14.0 
(40.65, 40.66]     NaN    NaN    NaN 
(40.66, 40.67]     NaN    NaN    NaN 
(40.67, 40.68]     NaN    NaN    7.0 
(40.68, 40.69]     NaN    NaN    NaN 
(40.69, 40.7]     NaN    1.0    1.0 
(40.7, 40.71]     1.0    1.0   3841.0 
(40.71, 40.72]     NaN    2.0   6537.0 
(40.72, 40.73]     NaN    NaN    NaN 
(40.73, 40.74]     9.0    2.0    NaN 
+2

你的指數是不是一個多指標。另外,'by'是'sort_index()'的關鍵字參數嗎?我沒有在文檔中看到它。發佈數據的'.head()'。這樣會更容易幫助。我無法將圖片複製/粘貼到python中。什麼定義最大和最小? –

+0

打算如何排序一對數字?什麼是「小」,什麼是這樣一對「大」? – Alexander

+0

@CoryMadden我發現它http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.sort_index.html我也更新了我的帖子 – madsthaks

回答

1

您可以reset indexsort by values

嘗試:

>>>plot_df_dropoff.reset_index().sort_values(by=["dropoff_latitude"], ascending=False) 

而作爲@JohnE提到的,你也可以只用sort_index():

>>>plot_df_dropoff.sort_index(ascending=False) 
+0

接受,如果這回答了你的問題。 –

相關問題