sumifs函数在python中 [英] sumifs function in python
问题描述
[['chr1','3088','1' ,744,'L1MCc_dup1']
['chr1','3089','1',744,'L1MCc_dup1']
['chr1','3090','1',744'' '1',96'MER63B']
['chr1','15038','1',96'MER63B'] $ $ b $'[' $ b $'['chr1','15039','1',96,'MER63B']
['chr1','15040','1',96,'MER63B']
[ 'chr1','19465','1',418,'MLT2B4_dup1']
['chr1','19466','1',418,'MLT2B4_dup1']
['chr1', '19467','1',418,'MLT2B4_dup1']]
在python中的 sumifs
函数(因为该文件对于excel来说太大了)根据列5中的标识符来求和列3的内容(输出可以是某个版本的 L1MCc_dup1
为3, MER63B
为4且 MLT2B4_dup1
是3)。
任何建议/帮助可以使这个功能成为可能?
使用字典:
<$ c对于my_list中的行,$ c> d = {}
:
key = row [4]
value = int(row [2])
d [key] = d.get (key,0)+ value
在这个循环之后, d
会将最后一列中的键值映射到所需的总和。
您也可以使用 collections.defaultdict
而不是普通的字典。
I have a list of lists that looks like:
[['chr1', '3088', '1', 744, 'L1MCc_dup1']
['chr1', '3089', '1', 744, 'L1MCc_dup1']
['chr1', '3090', '1', 744, 'L1MCc_dup1']
['chr1', '15037', '1', 96, 'MER63B']
['chr1', '15038', '1', 96, 'MER63B']
['chr1', '15039', '1', 96, 'MER63B']
['chr1', '15040', '1', 96, 'MER63B']
['chr1', '19465', '1', 418, 'MLT2B4_dup1']
['chr1', '19466', '1', 418, 'MLT2B4_dup1']
['chr1', '19467', '1', 418, 'MLT2B4_dup1']]
I need to make the equivalent of a sumifs
function in python (as the file is too big for excel) to sum the contents of column 3 based on the identifier in column 5 (output can be some version of L1MCc_dup1
is 3, MER63B
is 4 and MLT2B4_dup1
is 3).
Any advice/help to make this function?
Use a dictionary:
d = {}
for row in my_list:
key = row[4]
value = int(row[2])
d[key] = d.get(key, 0) + value
After this loop, d
will map the key values in the last column to the desired sums.
You could also use collections.defaultdict
instead of a normal dictionary.
这篇关于sumifs函数在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!