Python Forum
reindex dataframe after sorting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reindex dataframe after sorting
#1
It's a simple problem I'm sure, but I haven't found a solution.
I'm sorting a dataframe, then I need to reindex, starting from 1.
However, reindex undoes my previous sort.
>>> df
       MSRP        ASIN    Profit
0    $8.90   B07GRKMGCY  20254.70
1   $19.99   B0754FXQCM   9929.03
2   $16.49   B07QWQXW6J   8536.53
3  $239.95   B019GPCYDK      0.00
4   $18.99   B06XVV525Q  24283.00
5   $28.69   B079FM3MMD      0.00
>>> df.sort_values('Profit')
       MSRP        ASIN    Profit
3  $239.95   B019GPCYDK      0.00
5   $28.69   B079FM3MMD      0.00
2   $16.49   B07QWQXW6J   8536.53
1   $19.99   B0754FXQCM   9929.03
0    $8.90   B07GRKMGCY  20254.70
4   $18.99   B06XVV525Q  24283.00
>>> df.index = np.arange(1, len(df) + 1)
>>> df
       MSRP        ASIN    Profit
1    $8.90   B07GRKMGCY  20254.70
2   $19.99   B0754FXQCM   9929.03
3   $16.49   B07QWQXW6J   8536.53
4  $239.95   B019GPCYDK      0.00
5   $18.99   B06XVV525Q  24283.00
6   $28.69   B079FM3MMD      0.00
I expected this output instead:
       MSRP        ASIN    Profit
1  $239.95   B019GPCYDK      0.00
2   $28.69   B079FM3MMD      0.00
3   $16.49   B07QWQXW6J   8536.53
4   $19.99   B0754FXQCM   9929.03
5    $8.90   B07GRKMGCY  20254.70
6   $18.99   B06XVV525Q  24283.00
Reply
#2
Banged my head against the wall until I figured it out.
I had failed to save the dataframe after sorting it and I can sort and reset the index in the same line. then adjust it to start at one.
>>> df
     MSRP        ASIN    Profit
0    8.90  B07GRKMGCY  20254.70
1   19.99  B0754FXQCM   9929.03
2   16.49  B07QWQXW6J   8536.53
3  239.95  B019GPCYDK      0.00
4   18.99  B06XVV525Q  24283.00
5   28.69  B079FM3MMD      0.00
>>> df = df.sort_values(['Profit', 'MSRP']).reset_index(drop=True)
>>> df.index = np.arange(1, len(df) + 1)
>>> df
     MSRP        ASIN    Profit
1   28.69  B079FM3MMD      0.00
2  239.95  B019GPCYDK      0.00
3   16.49  B07QWQXW6J   8536.53
4   19.99  B0754FXQCM   9929.03
5    8.90  B07GRKMGCY  20254.70
6   18.99  B06XVV525Q  24283.00
Reply
#3
Yeah, reading the documentation of the methods often helps :-)
Reply
#4
(Jun-23-2019, 07:23 AM)ThomasL Wrote: Yeah, reading the documentation of the methods often helps :-)

Comments like that don't help.
Reply
#5
Instead of dropping the index, you can just set it:

df.sort_values(['Profit', 'MSRP'], inplace=True)
df.index = range(1, len(df) + 1)
(Jun-22-2019, 08:02 PM)Clunk_Head Wrote: I had failed to save the dataframe after sorting it
What does saving mean in this context? e.g. using df.to_csv or something else? Were you able to save your df before sorting?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reindex() Truman 2 3,101 Jul-17-2020, 10:21 PM
Last Post: Truman
  Why reindex converts integer to decimal? new_to_python 2 2,097 Feb-08-2020, 02:27 PM
Last Post: new_to_python
  Dataframe Rows Sorting stranger14u 1 2,568 Dec-17-2018, 11:47 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020