python - sort pandas DataFrame with a column with list
问题描述
I want to sort the rows of this dataframe based on the first value of the array in the "boxes" column 0.8299769, 0.04913859 in descending order. How can I do it.
classes boxes scores
0 7 [0.8299769, 0.27715018, 0.91143125, 0.54477763] 0.999721
2 7 [0.04913859, 0.35264254, 0.15933079, 0.64380944] 0.999397
解决方案
try this, create psuedo column sort
sort based on it & drop
df = pd.DataFrame({"classes": [7, 2], "boxes": [[0.04913859, 0.35264254, 0.15933079, 0.64380944],
[0.8299769, 0.27715018, 0.91143125, 0.54477763]],
"scores": [0.999721, 0.999397]})
df['sort'] = df['boxes'].apply(lambda x: x[0])
df.sort_values(by='sort', ascending=False).drop(columns=['sort'])
classes boxes scores
1 2 [0.8299769, 0.27715018, 0.91143125, 0.54477763] 0.999397
0 7 [0.04913859, 0.35264254, 0.15933079, 0.64380944] 0.999721
推荐阅读
- codeigniter - 在具有不同数据库的第一个 codeigniter 中使用相同的 codeigniter 文件会话
- embedded - Bootargs 使用 TFTP 和 QSPI 在 SAMA5D27-SOM1-EK 板上安装 Rootfs
- cakephp - 我的实体文件没有包含在 cakephp 中。有人能帮我吗?
- machine-learning - 构建自己的自定义面部识别的良好做法是什么?
- mysql - Mysql:如何在分区中使用 WHERE 条件
- ajax - 如何将此 Ajax 查询转换为 Angular 8 中的 http 客户端请求?
- javascript - 如何在点击时返回元素的内容?
- javascript - requireJS require 函数有一个没有定义的模块名
- java - 与 Windows Get-NetIPAddress 命令等效的 Java JNA API
- list - 如何获取 SharePoint 列表中的项目