python - Python - 从字典类型列熊猫数据框中获取前 5 项
问题描述
我有一个数据框,其中一列是字典,我在该字典中得到了大量的项目,这导致了我的内存问题。解决方案是只从该字典中获取前 10 个项目。我已经有了代码,但它给出了一个错误:
TypeError: '<' not supported between instances of 'dict' and 'dict'
我制作了一个示例代码只是为了向您展示我的问题:
import pandas as pd
import datetime
res = pd.DataFrame([])
res_tmp = pd.DataFrame([])
d = {'club': ['A1', 'B1'], 'score': [3, 4]}
df = pd.DataFrame(data=d)
for index, row in df.iterrows():
total = int(row['score']) * -1
res_tmp = res_tmp.append({'today': str(datetime.datetime.now()), 'total': total}, ignore_index=True)
res = res.append({'club': row['club'], 'details': res_tmp.to_dict('dict')},ignore_index=True)
res['details'] = res['details'].apply(lambda y: (sorted(y.items(), key=lambda x: x[1]))[:1])
我做错了什么?注意:在示例中,我只有两行,这就是为什么我放前 1 而不是前 10
谢谢!
解决方案
正如错误消息告诉您的那样,dicts 没有定义的值排序。如果要对字典进行排序,则必须提供您编写的函数来定义排序顺序。您提取了该值,但您还必须将 dict 转换为已<
定义的某种类型。例如:
key = lambda x: list(x[1].values())
推荐阅读
- c# - 如何将列表中字段的值与Where子句中另一个列表中的另一个值进行比较
- python - 比较两个多维列表的一些问题
- unity3d - 为什么 Image 和 SpriteRenderer 组件不是继承自一个通用基类?
- javascript - 为什么后退按钮会破坏我的 React 应用程序的某些部分?
- applescript - AppleScript 无法保存文本编辑文件
- python - 如何根据 Python 中的条件/断点从另一个列表中创建多个列表?
- amazon-web-services - 如何使用弹性 beantalk 和 aws 修复 Flask 应用程序中的内部服务器错误
- php - Codeignater,数据库中的数据全部为空
- excel - VBA 在选择中使用活动行号
- asp.net-mvc-4 - 使用模型重定向到响应视图不会保留模型属性