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

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

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

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

vi4fp9gy1#

一种解决方案可能是:

  1. data = pd.DataFrame({
  2. 'col': [[['58bcd029', 2, 'expert'],
  3. ['58bcd029', 2, 'user'],
  4. ['58bcd029', 2, 'expert']],
  5. [['58bcd029', 2, 'expert'],
  6. ['58bcd029', 2, 'expert']],
  7. [['1ee429fa', 1, 'expert']]]
  8. })
  9. print(data)
  10. col
  11. 0 [[58bcd029, 2, expert], [58bcd029, 2, user], [...
  12. 1 [[58bcd029, 2, expert], [58bcd029, 2, expert]]
  13. 2 [[1ee429fa, 1, expert]]
  14. data['count_expert'] = data['col'].apply(lambda x: [item for sublist in x for item in sublist].count('expert'))
  15. data['count_user'] = data['col'].apply(lambda x: [item for sublist in x for item in sublist].count('user'))
  16. data['ids'] = data['col'].apply(lambda x: set(sublist[0] for sublist in x))
  17. # For the purpose of illustration, I just selected these rows, but `col` is also there.
  18. print(data[['count_expert', 'count_user', 'ids']])
  19. count_expert count_user ids
  20. 0 2 1 {58bcd029}
  21. 1 2 0 {58bcd029}
  22. 2 1 0 {1ee429fa}
展开查看全部

相关问题