0

I'm attempting to sort the row indexes below from largest to smallest:

enter image description here

My first attempt was:

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

But I get the a Key Value Error.

Second thought based on this link didn't work either. It returned None.

This seems so simple but I can't figure it out. Any help would be much appreciated.

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

And here is a smaller segment that might be easier to work with:

                                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  
5
  • 2
    Your index isn't a multiindex. Also, is by a keyword argument for sort_index()? I don't see it in the docs. Post a .head() of your data. That would make it easier to help. I can't copy/paste a picture into python. What defines largest and smallest? Commented Aug 12, 2017 at 21:06
  • How do intend to sort a pair of numbers? What is "small", and what is "large" for such a pair? Commented Aug 12, 2017 at 21:15
  • @CoryMadden I found it pandas.pydata.org/pandas-docs/version/0.13.1/generated/… also I've updated my post Commented Aug 12, 2017 at 21:31
  • 1
    just do sort_index() without arguments. I think you confused pandas by essentially doing this in a redundant way. When you say sort_index() it already knows what the index is. The answer by @ogaga uzoh ought to be fine too, but not really necessary. You could also do sort_index(level=0) which is also redundant, but will work fine. Commented Aug 12, 2017 at 21:32
  • Thanks, assuming it isn't resolved I'll take a look when I get back. But I have things now. You're also using an old version of pandas. And I still don't understand what smallest and largest mean. Commented Aug 12, 2017 at 21:34

1 Answer 1

1

You can reset index and sort by values.

Try:

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

And as @JohnE mentioned, you can also just use sort_index():

>>>plot_df_dropoff.sort_index(ascending=False)
Sign up to request clarification or add additional context in comments.

1 Comment

Accept if this answered your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.