将列表中的选定项除以DataFrame中的另一列,然后选择顶部结果



我有以下测试DateFrame:

计数
标签 列表
冰淇淋 [['A',0.9],[[B',0.6],[[C',0.5],[[D',0.3],[[E',0.1]]
土豆 [['U',0.8],['V',0.7],[W',0.4],[X',0.3],[Y',0.2]]

Pandas无法以矢量方式处理列表。你别无选择,只能循环。最快的将是列表理解:

test['list'] = [[[a, b/len(l)] for a,b in l]
for l in test['list']]

或者,对于就地修改,一个简单的经典循环:

for l in test['list']:
for x in l:
x[1] /= len(l)

注意。您不需要";计数";列

输出:

tag                                               list
0  icecream  [[A, 0.18], [B, 0.12], [C, 0.1], [D, 0.06], [E...
1    potato  [[U, 0.16], [V, 0.14], [W, 0.08], [X, 0.06], [...

最新更新