reindex dataframe after sorting - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: reindex dataframe after sorting (/thread-19312.html) |
reindex dataframe after sorting - Clunk_Head - Jun-22-2019 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.00I 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 RE: reindex dataframe after sorting - Clunk_Head - Jun-22-2019 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 RE: reindex dataframe after sorting - ThomasL - Jun-23-2019 Yeah, reading the documentation of the methods often helps :-) RE: reindex dataframe after sorting - Clunk_Head - Jun-26-2019 (Jun-23-2019, 07:23 AM)ThomasL Wrote: Yeah, reading the documentation of the methods often helps :-) Comments like that don't help. RE: reindex dataframe after sorting - scidam - Jun-26-2019 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 itWhat does saving mean in this context? e.g. using df.to_csv or something else? Were you able to save your df before sorting?
|