Bottom Page

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
Quote
#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
Quote
#3
Yeah, reading the documentation of the methods often helps :-)
Quote
#4
(Jun-23-2019, 07:23 AM)ThomasL Wrote: Yeah, reading the documentation of the methods often helps :-)

Comments like that don't help.
Quote
#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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Dataframe Rows Sorting stranger14u 1 527 Dec-17-2018, 11:47 PM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)