如何在嵌套列表python中获取特定元素的计数

ws51t4hk  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(374)
count_freq   data
3            [['58bcd029', 2, 'expert'], 
              ['58bcd029', 2, 'user'], 
             ['58bcd029', 2, 'expert']]
2            [['58bcd029', 2, 'expert'], 
             ['58bcd029', 2, 'expert']]
1            [['1ee429fa', 1, 'expert']]

所以我想从每一行 Dataframe 和每一个列表中获得“Maven”和“用户”的计数。在获得Maven和用户数量后,我想将各自的ID存储在另一个列表中。我试着将它们转换成字典,并使用key进行计算,但它不起作用。谁能帮我做这个?
我希望 Dataframe 采用以下格式:

count_freq   count_expert  ids                     count_user ids
3            2             ['58bcd029','58bcd029'] 1          ['58bcd029']
2            2             ['58bcd029','58bcd029'] 0          []
1            1             ['1ee429fa']            0          []
vi4fp9gy

vi4fp9gy1#

一种解决方案可能是:

data = pd.DataFrame({
    'col': [[['58bcd029', 2, 'expert'],
             ['58bcd029', 2, 'user'],
             ['58bcd029', 2, 'expert']],
            [['58bcd029', 2, 'expert'],
             ['58bcd029', 2, 'expert']],
            [['1ee429fa', 1, 'expert']]]
})

print(data)
                                                 col
0  [[58bcd029, 2, expert], [58bcd029, 2, user], [...
1     [[58bcd029, 2, expert], [58bcd029, 2, expert]]
2                            [[1ee429fa, 1, expert]]

data['count_expert'] = data['col'].apply(lambda x: [item for sublist in x for item in sublist].count('expert'))
data['count_user'] = data['col'].apply(lambda x: [item for sublist in x for item in sublist].count('user'))
data['ids'] = data['col'].apply(lambda x: set(sublist[0] for sublist in x))

# For the purpose of illustration, I just selected these rows, but `col` is also there.

print(data[['count_expert', 'count_user', 'ids']])

   count_expert  count_user         ids
0             2           1  {58bcd029}
1             2           0  {58bcd029}
2             1           0  {1ee429fa}

相关问题