python - 使用集合中的 Counter 动态创建的字典的总和值

标签 python dictionary collections

我有这个 df:

import pandas as pd
a = [1,1,1,2,2,3,3,3,3,4,4,5,5,5]
b = ["pi","pi","k","J","pi","pi","k","k","J","pi","k","pi","k","pi"]
bin0 = [0,0,0,1,0,0,1,0,0,0,1,1,0,0]
bin1 = [1,1,1,0,1,0,0,1,1,0,0,0,1,0]
bin2 = [0,0,0,0,0,1,0,0,0,1,0,0,0,1]

df_test = pd.DataFrame({"a": a, "b": b,"bin0": bin0,"bin1": bin1,"bin2": 
bin2})

像这样:

    a   b  bin0  bin1  bin2
0   1  pi     0     1     0
1   1  pi     0     1     0
2   1   k     0     1     0
3   2   J     1     0     0
...
12  5   k     0     1     0
13  5  pi     0     0     1

然后我想从这个 df 创建字典,并对这些字典求和(如果它们具有相同的键):

from collections import Counter

thismodule = sys.modules[__name__]

df1 = df_test.groupby(['a', 'b']).agg({'b':'size', 'bin0':'sum', 
'bin1':'sum', 'bin2':'sum'}).rename(columns={'b': 'cant', 'bin0': 'b0', 
'bin1': 'b1', 'bin2': 'b2'}).reset_index(drop = False)


for evt in df1.a.unique():
    name1 = 'dict_'+str(evt)
    name2 = 'col_'+str(evt)
    df_ = df1
    df_ = df_[df_.a==evt].drop('a', 1).set_index('b').to_dict('index')
    setattr(thismodule, name1, df_)
    setattr(thismodule, name2, col_)  

获取,例如:

df_1 = {'k': {'cant': 1, 'b0': 0, 'b1': 1, 'b2': 0}, 'pi': {'cant': 2, 
'b0': 0, 'b1': 2, 'b2': 0}}

col_1 = Counter({'k': {'cant': 1, 'b0': 0, 'b1': 1, 'b2': 0}, 'pi': 
{'cant': 2, 'b0': 1, 'b1': 0, 'b2': 1}})

最后,当我想对具有相同键的字典的值求和时,出现错误:

col_1 = eval("col_1")
col_2 = eval("col_2")

sumdict = col_1 +col_2
print(sumdict)

错误是:

newcount = count + other[elem]

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

最佳答案

这不是你想要实现的目标吗:

df_test.groupby(['a', 'b']).sum().reset_index().groupby('a').sum()

    bin0    bin1    bin2
a           
1   0   3   0
2   1   1   0
3   1   2   1
4   1   0   1
5   1   1   1

关于python - 使用集合中的 Counter 动态创建的字典的总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727463/

相关文章:

python - 将列转换为标题行

python - 在我的 .html 文件中使用 python 切片字符串

c++ - 尝试初始化将对象作为构造函数中的值的映射

在 C 中创建具有硬编码值的字符串数组

java - 如何按枚举的顺序对 List<Enum, Collection> 进行排序?

c# - C# 中不可修改的列表

python - 通过 Python 2.7 在 .xls 上写入多列

python - 从列表生成嵌套字典

MongoDb 时间戳

python - 使用 python 替换 CSV 文件中的值