python-3.x - 如何使用列表单元格从列表中查看 DataFrame
问题描述
我想检查x_list
列 [a] 列表单元格中的值,如果在字典中找到x_dic
填充c
值,例如第 1 行将返回此字典:
x_dic = {'1':, '2':[130], '3':[130], '4':, '5':[130]}
这是我的代码:
x_list = ['4', '2', '3', '5', '1']
x_dic = {'1':, '2':, '3':, '4':, '5':}
df =
a b c
0 [1, 3, 4, 2, 5] 10 120
1 [2, 5, 3] 15 130
2 [1, 2, 3, 4] 20 140
3 [1, 2] 25 110
df.a.astype(str)
for g in df['a']:
for i in x_list:
if i in g:
x_dic[i] += c
x_dic
TypeError:“int”对象不可迭代
解决方案
推荐阅读
- python - 在 Python 中而不是在 O(1) 中追加
- postgresql - 我是否需要通过 RDS IAM 身份验证每 15 分钟重新连接一次
- css - 在代码和事件处理程序上更改 JavaFX 的样式
- github - GitHub Actions 上传和下载工件
- r - 使用 read_csv 导入大型 csv 文件时丢失数据列
- python - 如何处理 django 应用程序中的管理命令异常?
- node.js - Create-React-App (CRA) 是否与 Express 是 Node 应用程序一样是 Node 应用程序?
- java - 当属性名称包含前面的单个字母时,JPA 存储库方法不会取消大写属性名称
- r - 单击操作按钮后,R Shiny 仅渲染数据表
- html - 带有美化功能的 Vscode 不会缩进 django html 模板