pandas 如何在字典列表中创建一个新的键,它是另一个键的总和?

gmxoilav  于 2023-08-01  发布在  其他
关注(0)|答案(6)|浏览(99)

我有一本这样的字典:

[
    {'name': 'a', 'age': '20', 'value': '10'},
    {'name': 'a', 'age': '30', 'value': '15'},
    {'name': 'a', 'age': '40', 'value': '25'},
    {'name': 'b', 'age': '20', 'value': '11'},
    {'name': 'b', 'age': '30', 'value': '12'},
    {'name': 'b', 'age': '40', 'value': '13'}
]

字符串
现在,我想添加一个新的键,该键包含相同名称的所有值的总和,如下所示:

[
    {'name': 'a', 'age': '20', 'value': '10', 'sum': '50'},
    {'name': 'a', 'age': '30', 'value': '15', 'sum': '50'},
    {'name': 'a', 'age': '40', 'value': '25', 'sum': '50'},
    {'name': 'b', 'age': '20', 'value': '11', 'sum': '36'},
    {'name': 'b', 'age': '30', 'value': '12', 'sum': '36'},
    {'name': 'b', 'age': '40', 'value': '13', 'sum': '36'}
]


我已经在使用Pandas进行分组和聚合。Pandas也可以这样做吗?还是有标准的Python变体?

ha5z0ras

ha5z0ras1#

下面的代码使用一个新的“sum”键更新列表中的每个字典,该键是每个字典中具有相同“name”键的值的总和。

your_list = [
    {'name': 'a', 'age': '20', 'value': '10'}, 
    {'name': 'a', 'age': '30', 'value': '15'},
    {'name': 'a', 'age': '40', 'value': '25'},  
    {'name': 'b', 'age': '20', 'value': '11'}, 
    {'name': 'b', 'age': '30', 'value': '12'},
    {'name': 'b', 'age': '40', 'value': '13'},
    ...
]

previous = {}
for obj in your_list:
    name = obj["name"]
    
    # Check if we already calculated the sum
    value_sum = previous.get(name)
    if value_sum is None:
        value_sum = sum(int(d["value"]) for d in your_list if d["name"] == name)
        previous[name] = value_sum
    
    obj["sum"] = str(value_str)  # If you want it as type 'str'

print(your_list)

字符串
输出量:

>>>
{'name': 'a', 'age': '20', 'value': '10', 'sum': '50'}
{'name': 'a', 'age': '30', 'value': '15', 'sum': '50'}
{'name': 'a', 'age': '40', 'value': '25', 'sum': '50'}
{'name': 'b', 'age': '20', 'value': '11', 'sum': '36'}
{'name': 'b', 'age': '30', 'value': '12', 'sum': '36'}
{'name': 'b', 'age': '40', 'value': '13', 'sum': '36'}
...

cl25kdpy

cl25kdpy2#

这个就行了。

list_of_dicts = [
{'key1': 10, 'key2': 20},
{'key1': 5, 'key2': 15},
{'key1': 8, 'key2': 12}]

for idx in range(len(list_of_dicts)):
    list_of_dicts[idx].update({'sum_keys': sum(list_of_dicts[idx].values())})

字符串


的数据

sqougxex

sqougxex3#

是的,你可以使用Pandas来实现这一点。下面是一个示例代码:

import pandas as pd

# create the dictionary
data = [{'name': 'a', 'age': '20', 'value': '10'},
        {'name': 'a', 'age': '30', 'value': '15'},
        {'name': 'a', 'age': '40', 'value': '25'},
        {'name': 'b', 'age': '20', 'value': '11'},
        {'name': 'b', 'age': '30', 'value': '12'},
        {'name': 'b', 'age': '40', 'value': '13'}]

# create a DataFrame from the dictionary
df = pd.DataFrame(data)

# convert the 'value' column to numeric values
df['value'] = pd.to_numeric(df['value'])

# create a new column 'sum' with the sum of 'value' for each 'name'
df['sum'] = df.groupby('name')['value'].transform('sum')

# convert the 'sum' column to string values
df['sum'] = df['sum'].astype(str)

# convert the DataFrame back to a list of dictionaries
result = df.to_dict(orient='records')

print(result)

字符串
这段代码从字典中创建一个Pandas DataFrame,将'value'列转换为数值,按'name'对DataFrame进行分组,计算每个组的'value'之和,并为每个'name'创建一个新的列'sum'。最后,它将“sum”列转换为字符串值,并将DataFrame转换回字典列表。
输出应该是一个字典的列表,每个“名称”都添加了“sum”列。

lxkprmvk

lxkprmvk4#

另一个解决方案,没有pandas,使用纯Python:

lst = [
    {"name": "a", "age": "20", "value": "10"},
    {"name": "a", "age": "30", "value": "15"},
    {"name": "a", "age": "40", "value": "25"},
    {"name": "b", "age": "20", "value": "11"},
    {"name": "b", "age": "30", "value": "12"},
    {"name": "b", "age": "40", "value": "13"},
]

out = {}
for d in lst:
    out.setdefault(d["name"], []).append(d)

tmp = {k: sum(int(i["value"]) for i in v) for k, v in out.items()}

out = [{**v, "sum": str(tmp[v["name"]])} for v in lst]
print(out)

字符串
图纸:

[
    {"name": "a", "age": "20", "value": "10", "sum": "50"},
    {"name": "a", "age": "30", "value": "15", "sum": "50"},
    {"name": "a", "age": "40", "value": "25", "sum": "50"},
    {"name": "b", "age": "20", "value": "11", "sum": "36"},
    {"name": "b", "age": "30", "value": "12", "sum": "36"},
    {"name": "b", "age": "40", "value": "13", "sum": "36"},
]

mrphzbgm

mrphzbgm5#

若要在字典列表中创建一个新的键,该键是另一个键的总和,您可以遍历列表中的每个字典并计算所需键的总和。然后,向每个字典中添加一个新的键和计算的总和。下面是一个Python示例,假设你想添加一个名为'sum_key'的新键,它包含'key1'和'key2'的总和:

# Example list of dictionaries
list_of_dicts = [
    {'key1': 10, 'key2': 20},
    {'key1': 5, 'key2': 15},
    {'key1': 8, 'key2': 12}
]

# Iterate through the list and calculate the sum of 'key1' and 'key2'
for dictionary in list_of_dicts:
    dictionary['sum_key'] = dictionary['key1'] + dictionary['key2']

# Resulting list of dictionaries with the new 'sum_key' added
print(list_of_dicts)

字符串
输出将为:

[
    {'key1': 10, 'key2': 20, 'sum_key': 30},
    {'key1': 5, 'key2': 15, 'sum_key': 20},
    {'key1': 8, 'key2': 12, 'sum_key': 20}
]


现在列表中的每个字典都有一个新的键“sum_key”,其中包含“key1”和“key2”的总和。

hmtdttj4

hmtdttj46#

另一个没有pandas但有defaultdict的选项:

from collections import defaultdict

data = [{'name': 'a', 'age': '20', 'value': '10'},
        {'name': 'a', 'age': '30', 'value': '15'},
        {'name': 'a', 'age': '40', 'value': '25'},
        {'name': 'b', 'age': '20', 'value': '11'},
        {'name': 'b', 'age': '30', 'value': '12'},
        {'name': 'b', 'age': '40', 'value': '13'}]

temp = defaultdict(int)

for d in data:
    temp[d['name']] += int(d['value'])

for d in data:
    d['sum'] = str(temp[d['name']])

print(data)

字符串
。或者没有违约判决。。

temp = {}

for d in data:
    name = d['name']
    temp[name] = temp.get(name, 0) + int(d['value'])

for d in data:
    d['sum'] = str(temp[d['name']])

相关问题