标签:country pandas test 字典 value taf and group 列表
需求计算 列表内 名字一样的 value 值
name = [
{‘name‘: ‘he‘, ‘value‘: 100, ‘ps‘: ‘test1‘},
{‘name‘: ‘quan‘, ‘value‘: 150, ‘ps‘: ‘test2‘},
{‘name‘: ‘he‘, ‘value‘: 100, ‘ps‘: ‘test3‘},
{‘name‘: ‘quan‘, ‘value‘: 200, ‘ps‘: ‘test4‘}
]
pip install pandas
import pandas as pd
df = pd.DataFrame(name)
cols = [‘name‘, ‘value‘]
df = df.ix[:, cols]
result = df.groupby([‘name‘]).sum()
dict_country = result.T.to_dict(‘list‘)
print(dict_country)
{‘he‘: [200], ‘quan‘: [350]}
标签:country pandas test 字典 value taf and group 列表
原文地址:http://blog.51cto.com/hequan/2178785