在Python中用条件求和数组

2024-06-14 19:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大的数据数组。我想用一个或两个条件对列求和。数据已经作为类存储在字典中。你知道吗

数据相当广泛,但重要的部分是这样的

[["Gothenburg", "2018-01-05", "jan", 1.5, 2.3, 107],
 ["Gothenburg", "2018-01-15", "jan", 1.3, 3.3, 96],
 ["Gothenburg", "2018-01-25", "jan", 1.7, 3.2, 45],
 ["Gothenburg", "2018-03-05", "mar", 1.5, 2.1, 96],
 ["Gothenburg", "2018-03-05", "mar", 1.9, 2.8, 102],
 ["Malmo", "2018-01-02", "jan", 1.6, 2.3, 104],
 ["Malmo", "2018-01-10", "jan", 1.0, 2.9, 112],
 ["Malmo", "2018-03-05", "mar", 0.7, 4.3, 151],
 ["Malmo", "2018-03-25", "mar", 1.0, 3.3, 98],
 ["Hallsberg", "2018-01-25", "jan", 2.5, 2.3, 87],
 ["Hallsberg", "2018-02-14", "feb", 2.2, 2.3, 168],
 ["Hallsberg", "2018-03-06", "mar", 3.7, 2.3, 142],
 ["Hallsberg", "2018-04-29", "apr", 2.7, 2.3, 100]]

栏目说明: 0=城市,1=日期,2=月份,3=平均值1,4=平均值2,5=平均值3

这个阵列总共大约有8000行,可能有300个不同的城市。你知道吗

我想要实现的是在第0、1、2列的值之后对第3、4、5列求和。你知道吗

例如,键为“Malmo”的第3列的和=1.6+1.0+0.7+1.0=4.3 键为“Malmo”和“jan”的第3列之和=1.6+1.0=2.6

这些条件和可以存储在字典(或更好的解决方案)中,也可以显示在屏幕上。你知道吗

我想有一个聪明的方法很容易做到这一点,但我还没想明白。我试过使用for循环和if案例,但是很混乱。希望能在这里得到一些好的建议!你知道吗


Tags: 数据字典数组解决方案条件aprmarjan
2条回答

诀窍是使用元组作为字典的键。假设数据存储在名为big_array_with_data的变量中,下面是使用collections.defaultdict的解决方案:

from collections import defaultdict

monthly = [defaultdict(int) for i in range(3)]
totals =  [defaultdict(int) for i in range(3)]

for place, _, month, *means in big_array_with_data:
    for i, mean in enumerate(means):
        monthly[i][(place, month)] += mean
        totals[i][place] += mean

print(monthly[0][('Malmo', 'jan')])
print(totals[0]['Malmo'])

你也可以不用defaultdict这样做:

monthly[i][(place, month)] = monthly[i].get((place, month), 0) + mean

也就是说,如果你计划定期做这样的数据处理,那么学习熊猫教程是一个很好的时间投入。你知道吗

我喜欢将pandas库用于数据帧类型的对象。您的问题解决方案:

import pandas as pd 
df  = pd.DataFrame([["Gothenburg", "2018-01-05", "jan", 1.5, 2.3, 107],
 ["Gothenburg", "2018-01-15", "jan", 1.3, 3.3, 96],
 ["Gothenburg", "2018-01-25", "jan", 1.7, 3.2, 45],
 ["Gothenburg", "2018-03-05", "mar", 1.5, 2.1, 96],
 ["Gothenburg", "2018-03-05", "mar", 1.9, 2.8, 102],
 ["Malmo", "2018-01-02", "jan", 1.6, 2.3, 104],
 ["Malmo", "2018-01-10", "jan", 1.0, 2.9, 112],
 ["Malmo", "2018-03-05", "mar", 0.7, 4.3, 151],
 ["Malmo", "2018-03-25", "mar", 1.0, 3.3, 98],
 ["Hallsberg", "2018-01-25", "jan", 2.5, 2.3, 87],
 ["Hallsberg", "2018-02-14", "feb", 2.2, 2.3, 168],
 ["Hallsberg", "2018-03-06", "mar", 3.7, 2.3, 142],
 ["Hallsberg", "2018-04-29", "apr", 2.7, 2.3, 100]])

df.columns = ['City', 'Date', 'Month', 'Mean1', 'Mean2', 'Mean3']

选择分组依据:

group_by = ['City', 'Month'] #group_by = ['Month']

使用以下列的总和创建一个分组\u by Dataframe:

City_Mon_Sum = df.groupby(group_by).agg({'Mean1': 'sum', 'Mean2': 'sum', 'Mean3': 'sum'}).reset_index()
City_Mon_Sum.rename(columns = {'Mean1': 'Group_Mean1', 'Mean2': 'Group_Mean2', 'Mean3': 'Group_Mean3'}, inplace = True )

合并两个数据帧:

df = pd.merge(df, City_Mon_Sum, on = group_by)

输出:

City    Date    Month   Mean1   Mean2   Mean3   Group_Mean1 Group_Mean2 Group_Mean3
0   Gothenburg  2018-01-05  jan 1.5 2.3 107           4.5   8.8          248
1   Gothenburg  2018-01-15  jan 1.3 3.3 96  4.5 8.8 248
2   Gothenburg  2018-01-25  jan 1.7 3.2 45  4.5 8.8 248
3   Gothenburg  2018-03-05  mar 1.5 2.1 96             3.4  4.9          198
4   Gothenburg  2018-03-05  mar 1.9 2.8 102 3.4 4.9 198
5   Malmo   2018-01-02  jan 1.6 2.3 104 2.6 5.2 216
6   Malmo   2018-01-10  jan 1.0 2.9 112 2.6 5.2 216
7   Malmo   2018-03-05  mar 0.7 4.3 151 1.7 7.6 249
8   Malmo   2018-03-25  mar 1.0 3.3 98  1.7 7.6 249
9   Hallsberg   2018-01-25  jan 2.5 2.3 87  2.5 2.3 87
10  Hallsberg   2018-02-14  feb 2.2 2.3 168 2.2 2.3 168
11  Hallsberg   2018-03-06  mar 3.7 2.3 142 3.7 2.3 142
12  Hallsberg   2018-04-29  apr 2.7 2.3 100 2.7 2.3 100

相关问题 更多 >