在列表字典中添加元素

时间:2015-08-29 00:13:42

标签: python python-2.7

我有以下字典:

dic = {title: ['a', 'a', 'a', 'b', 'b', 'c', 'c'], avg1: [1, 2, 3, 4, 5, 6, 7], avg2: [2, 2, 3, 4, 7, 9, 7]} 

我想在列表中添加项目" avg1"和" avg2"基于列表中的项目" title"。结果dic将是:

dic = {title: ['a', 'b', 'c'], avg1: [6, 9, 13], avg2: [7, 11, 16]}

1 个答案:

答案 0 :(得分:1)

我建议你重新考虑一下你的数据结构,你存储信息的方式有点尴尬。下面可以做你想要的熊猫,但它是熊猫的笨拙用法(我的意思是最后一行)

import pandas as pd

>>> d = {title: ['a', 'a', 'a', 'b', 'b', 'c', 'c'], avg1: [1, 2, 3, 4, 5, 6, 7], avg2: [2, 2, 3, 4, 7, 9, 7]}

>>> df = pd.DataFrame(d)
>>> df
   avg1  avg2 title
   0     1     2     a
   1     2     2     a
   2     3     3     a
   3     4     4     b
   4     5     7     b
   5     6     9     c
   6     7     7     c
>>> g = df.groupby('title')

>>> g.avg1.apply(sum)
title
a         6
b         9
c        13
Name: avg2, dtype: int64

>>> g.avg2.apply(sum)
title
a         7
b        11
c        16
Name: avg2, dtype: int64

# An very awkward use of pandas
>>> dict(title=g.avg1.apply(sum).index.values.tolist(),
>>>      avg1=g.avg1.apply(sum).values.tolist(),
>>>      avg2=g.avg2.apply(sum).values.tolist())
{'avg1': [6, 9, 13], 'avg2': [7, 11, 16], 'title': ['a', 'b', 'c']}