以绝对值对熊猫系列进行排序

时间:2015-12-07 12:23:24

标签: python numpy pandas

我有一个像下面这样的熊猫系列:

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]

我想按降序对此系列进行排序,以便最高绝对值功能应该排在最前面。所以这里所有权重为3+的特征无论是负面的还是正面都会出现在顶部,然后是所有的2等等。也希望相应的实际值与名称一起出现

请告知

1 个答案:

答案 0 :(得分:2)

对您可以执行以下操作的值进行排序

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

如果您想对值进行排序并保留与原始系列中相同的数据,则可以执行以下操作

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