首页 > 解决方案 > 如何使用列表单元格从列表中查看 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-3.xdataframe

解决方案


推荐阅读