pandas - 如何根据数据框中未使用 if 条件的值对 14 列进行排序,而不是按行排序?
问题描述
我正在研究来自测试图像的预测数据框。我有一个包含 14 个值的列表,每个值都是类。我想比较模型预测在不同概率上的性能。现在我希望将这些值放在一个带有名称的新数据框中。之后,我想让它们按降序排序,如 10、9、8、7、6、5、4、3、2、1 等,如何对这些列进行排序并就地更改?示例数据:
col_name = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion', 'Emphysema', 'Fibrosis', 'Hernia',
'Infiltration', 'Mass', 'Nodule', 'Pleural_Thickening', 'Pneumonia', 'Pneumothorax']
[0.2268, 0.0717, 0.2414, 0.0845, 0.2153, 0.2802, 0.3154, 0.2922, 0.1558,
0.3638, 0.2032, 0.1259, 0.2359, 0.4329]
我不希望按行排序不向数据框添加新的 col 我只是根据列值排序,较大的列值将位于左侧,如上所示,我展示了一个示例。谢谢
解决方案
从以下数据框:
>>> import pandas as pd
>>> l_data_raw = [[0.2268, 0.0717, 0.2414, 0.0845, 0.2153, 0.2802, 0.3154, 0.2922, 0.1558, 0.3638, 0.2032, 0.1259, 0.2359, 0.4329],
... [0.0468, 0.7172, 0.4143, 0.8452, 0.1534, 0.8025, 0.1546, 0.9227, 0.5583, 0.3387, 0.2325, 0.3259, 0.3597, 0.3259],
... [0.2683, 0.0173, 0.4154, 0.8475, 0.7153, 0.2027, 0.3543, 0.2752, 0.7558, 0.3853, 0.3752, 0.7359, 0.8559, 0.2958],
... [0.5788, 0.5617, 0.2754, 0.7645, 0.7853, 0.4602, 0.8754, 0.2332, 0.8768, 0.3768, 0.2782, 0.7659, 0.7859, 0.3729]]
>>> df = pd.DataFrame(data=l_data_raw, index=[0, 1, 2, 3], columns=['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion', 'Emphysema', 'Fibrosis', 'Hernia', 'Infiltration', 'Mass', 'Nodule', 'Pleural_Thickening', 'Pneumonia', 'Pneumothorax'])
>>> df
Atelectasis Cardiomegaly Consolidation Edema Effusion Emphysema Fibrosis Hernia Infiltration Mass Nodule Pleural_Thickening Pneumonia Pneumothorax
0 0.2268 0.0717 0.2414 0.0845 0.2153 0.2802 0.3154 0.2922 0.1558 0.3638 0.2032 0.1259 0.2359 0.4329
1 0.0468 0.7172 0.4143 0.8452 0.1534 0.8025 0.1546 0.9227 0.5583 0.3387 0.2325 0.3259 0.3597 0.3259
2 0.2683 0.0173 0.4154 0.8475 0.7153 0.2027 0.3543 0.2752 0.7558 0.3853 0.3752 0.7359 0.8559 0.2958
3 0.5788 0.5617 0.2754 0.7645 0.7853 0.4602 0.8754 0.2332 0.8768 0.3768 0.2782 0.7659 0.7859 0.3729
我们可以然后按如下值重新排序每个图像组stack
:DataFrame
>>> df_stacked = df.stack().to_frame().reset_index(drop=False)
>>> df_stacked.columns = ['image', 'class', 'value']
>>> df_agg = df_stacked.groupby(['image', 'class']).agg({'value':sum})
>>> df_agg['value'].groupby('image', group_keys=False).nlargest(14).to_frame()
value
image class
0 Pneumothorax 0.4329
Mass 0.3638
Fibrosis 0.3154
Hernia 0.2922
Emphysema 0.2802
Consolidation 0.2414
Pneumonia 0.2359
Atelectasis 0.2268
Effusion 0.2153
Nodule 0.2032
Infiltration 0.1558
Pleural_Thickening 0.1259
Edema 0.0845
Cardiomegaly 0.0717
1 Hernia 0.9227
Edema 0.8452
Emphysema 0.8025
Cardiomegaly 0.7172
Infiltration 0.5583
Consolidation 0.4143
Pneumonia 0.3597
Mass 0.3387
Pleural_Thickening 0.3259
Pneumothorax 0.3259
Nodule 0.2325
Fibrosis 0.1546
Effusion 0.1534
Atelectasis 0.0468
2 Pneumonia 0.8559
Edema 0.8475
Infiltration 0.7558
Pleural_Thickening 0.7359
Effusion 0.7153
Consolidation 0.4154
Mass 0.3853
Nodule 0.3752
Fibrosis 0.3543
Pneumothorax 0.2958
Hernia 0.2752
Atelectasis 0.2683
Emphysema 0.2027
Cardiomegaly 0.0173
3 Infiltration 0.8768
Fibrosis 0.8754
Pneumonia 0.7859
Effusion 0.7853
Pleural_Thickening 0.7659
Edema 0.7645
Atelectasis 0.5788
Cardiomegaly 0.5617
Emphysema 0.4602
Mass 0.3768
Pneumothorax 0.3729
Nodule 0.2782
Consolidation 0.2754
Hernia 0.2332
推荐阅读
- firebase - 在 Flutter / Dart 的工厂函数中运行异步代码
- reactjs - 如何获取自定义 React/React-Native 组件的参数和类型定义
- python - Discord.py 获取没有机器人的服务器成员
- r - R 中的 Vemco 声学遥测数据(vrl 文件)
- excel - 将 Workbook_1 中的值记录到 Workbook_2
- typescript - 如何使打字稿在失败的非空断言上抛出运行时错误?
- r - 在某些条件下转换向量中的矩阵值
- jquery - toggleClass 单击或更改子输入
- html - 开发 FlexdashBoard 应用程序时如何在 renderUI 中结合数据表和 HTML。?
- java - 带有本地数据库中用户的带有弹簧启动的 Azure AD