2

I have a pandas series like below:

admission_age_inyears                                           [3.86703690989]
emergencydepartmentlengthofstayminutes                          [3.84708584711]
current_los_from_admissions                                     [3.83956976064]
total_time_in_progressive_inpatient                             [3.63955027973]
total_time_spent_inpatient                                      [2.59339330312]
nbr_of_hosp_last_90_days                                        [2.44570139977]
total_time_spent_in_er                                          [2.37914969651]
prior_admittype_emergency                                       [2.18467109815]
nbr_inpatient_visits                                            [2.09615621507]
curr_rx_gen_atorvastatin_calcium                                [2.08752966479]
substanceusehistory       
curr_rx_gen_tranexamic_acid                                    [-1.87352757522]
total_current_thera_ana                                        [-1.89007660143]
total_surgery_duration                                         [-2.50947580961]
avg_surgery_duration                                           [-2.58916844719]
curr_rx_gen_celecoxib                                           [-2.6594131822]
curr_rx_gen_propofol_iv                                        [-3.04498593439]
curr_rx_gen_fentanyl_citrate                                   [-3.14017330213]
curr_rx_gen_ketorolac_tromethamine                             [-3.14424766125]
curr_rx_gen_acetaminophen                                      [-3.47925239986]
tot_est_median_income_dollars                                  [-3.59465137553]
curr_rx_gen_midazolam_hcl                                      [-3.73260903286]

I want to sort this series in descending order such that the highest absolute value feature should come on top. So here all the feature which have weights as 3+ irrespective if that is negative or positive will come on top , then all 2's and so on.Also want the corresponding actual values to be present along with the names

Please advise

Baktaawar
  • 7,086
  • 24
  • 81
  • 149
  • 1
    Can you post raw input data and code to reproduce your df, it's unclear if your values are float or lists with a single float element or a string – EdChum Dec 07 '15 at 12:24
  • It's a series with index of series as the feature names and column of the series as list of value.Its a list with single float element corresponding to each index feature – Baktaawar Dec 07 '15 at 12:25
  • 5
    See here - http://stackoverflow.com/questions/30486263/pandas-sort-by-absolute-value-without-changing-the-data – Tom Ron Dec 07 '15 at 12:35
  • Yet another way `df.iloc[df.col_name.abs().argsort()]` :) – matjazzz144 Dec 07 '15 at 15:28

1 Answers1

5

to sort the values you can do the following

In [95]:
order = S.map(lambda x : x[0]).abs().sort_values(ascending = False)
order
Out[95]:
admission_age_inyears                     3.867037
emergencydepartmentlengthofstayminutes    3.847086
current_los_from_admissions               3.839570
curr_rx_gen_midazolam_hcl                 3.732609
total_time_in_progressive_inpatient       3.639550
tot_est_median_income_dollars             3.594651
curr_rx_gen_acetaminophen                 3.479252
curr_rx_gen_ketorolac_tromethamine        3.144248
curr_rx_gen_fentanyl_citrate              3.140173
curr_rx_gen_propofol_iv                   3.044986
curr_rx_gen_celecoxib                     2.659413
total_time_spent_inpatient                2.593393
avg_surgery_duration                      2.589168
total_surgery_duration                    2.509476
nbr_of_hosp_last_90_days                  2.445701
total_time_spent_in_er                    2.379150
prior_admittype_emergency                 2.184671
nbr_inpatient_visits                      2.096156
curr_rx_gen_atorvastatin_calcium          2.087530
total_current_thera_ana                   1.890077
curr_rx_gen_tranexamic_acid               1.873528
substanceusehistory                            NaN
dtype: float64

if you want to sort the values and keep the same data as in your original series you can do the following

In [96]:
order.index
S[order.index]
Out[96]:
admission_age_inyears                      [3.86703690989]
emergencydepartmentlengthofstayminutes     [3.84708584711]
current_los_from_admissions                [3.83956976064]
curr_rx_gen_midazolam_hcl                 [-3.73260903286]
total_time_in_progressive_inpatient        [3.63955027973]
tot_est_median_income_dollars             [-3.59465137553]
curr_rx_gen_acetaminophen                 [-3.47925239986]
curr_rx_gen_ketorolac_tromethamine        [-3.14424766125]
curr_rx_gen_fentanyl_citrate              [-3.14017330213]
curr_rx_gen_propofol_iv                   [-3.04498593439]
curr_rx_gen_celecoxib                      [-2.6594131822]
total_time_spent_inpatient                 [2.59339330312]
avg_surgery_duration                      [-2.58916844719]
total_surgery_duration                    [-2.50947580961]
nbr_of_hosp_last_90_days                   [2.44570139977]
total_time_spent_in_er                     [2.37914969651]
prior_admittype_emergency                  [2.18467109815]
nbr_inpatient_visits                       [2.09615621507]
curr_rx_gen_atorvastatin_calcium           [2.08752966479]
total_current_thera_ana                   [-1.89007660143]
curr_rx_gen_tranexamic_acid               [-1.87352757522]
substanceusehistory                                  [nan]
dtype: object
Nader Hisham
  • 5,214
  • 4
  • 19
  • 35