gpt4 book ai didi

python - 按绝对值对 pandas 系列进行排序

转载 作者:行者123 更新时间:2023-11-30 23:03:21 24 4
gpt4 key购买 nike

我有一个如下所示的 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,依此类推。还希望相应的实际值与名称一起出现

请指教

最佳答案

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

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

关于python - 按绝对值对 pandas 系列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34133494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com