简体   繁体   English

熊猫数据框-在列中的列表中查找总和

[英]Pandas Dataframe - find sums in list in column

I have a list like 我有一个类似的清单

source = [{'TGA': [0, 1, 0, 0], 'AAC': [0, 0, 0, 1], 'GAA': [0, 0, 1, 0], 
           'GTG': [1, 0, 0, 0]},{'TGA': [0, 1, 0, 0], 'AAC': [0, 0, 0, 1], 
           'GAA': [0, 0, 1, 0], 'GTG': [1, 0, 0, 0]} ]

I need to sum every digit in list column: 我需要对列表列中的每个数字求和:

pandas.DataFrame(source)
        AAC           GAA           GTG           TGA
  0  [0, 0, 0, 1]  [0, 0, 1, 0]  [1, 0, 0, 0]  [0, 1, 0, 0]
  1  [0, 0, 0, 1]  [0, 0, 1, 0]  [1, 0, 0, 0]  [0, 1, 0, 0]`

And in final: 最后:

         AAC           GAA           GTG           TGA
    sum  [0, 0, 0, 2 ] [0, 0, 2, 0] [2, 0, 0, 0]  [0, 2, 0, 0]

How can I do this? 我怎样才能做到这一点?

You can use this to sum a list of dict of list: 您可以使用它来汇总列表的字典列表:

source = [{'TGA': [0, 1, 0, 0], 'AAC': [0, 0, 0, 1], 'GAA': [0, 0, 1, 0],
           'GTG': [1, 0, 0, 0]},{'TGA': [0, 1, 0, 0], 'AAC': [0, 0, 0, 1],
           'GAA': [0, 0, 1, 0], 'GTG': [1, 0, 0, 0]} ]

res = {}
for d in source:
    for key,value in d.items():
            if key not in res:
                    res[key] = value
            else:
                    res[key] = [x+y for x,y in zip(res[key],value) ]

print res

You can easily change the entries into numpy.array s, then sum: 您可以轻松地将条目更改为numpy.array ,然后求和:

import numpy as np

>> df.applymap(np.array).sum()
AAC    [0, 0, 0, 2]
GAA    [0, 0, 2, 0]
GTG    [2, 0, 0, 0]
TGA    [0, 2, 0, 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫数据框-在A列中每个标签的B列中求和 - Pandas Dataframe - find sums in column B across each label in column A pandas DataFrame 的列总和(但保留 pandas DataFrame 的结构) - Column sums of a pandas DataFrame (but keep the structure of pandas DataFrame) 在 Pandas 数据框列中查找列表的最大值 - Find the max of a list in a Pandas dataframe column 在另一列的列表中查找与 Pandas 数据框列最近的元素 - Find closest element of a pandas dataframe column in another column's list Pandas Dataframe 按列索引范围对列进行分组并获取组总和 - Pandas Dataframe grouping columns by column index ranges and get group sums 在一个列表中查找项目,但不在熊猫数据框列中的另一个列表中查找项目 - Find items in one list but not in another in a pandas dataframe column 查找给定列中单词列表的总出现次数(SQL/Pandas DataFrame) - find total occurences of a list of words in a given column (SQL/Pandas DataFrame) 查找包含整数列的 pandas dataframe 与包含整数列表列的 dataframe 匹配的所有实例 - Find all instances where a pandas dataframe containing a column of int's matches a dataframe with a column of a list of ints 使用带有列表的列对 pandas DataFrame 进行排序 - sort pandas DataFrame with a column with list Pandas Dataframe 列到 python 列表 - Pandas Dataframe column to python list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM