Jun-22-2019, 06:53 PM
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.
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
