什么是Excel的SUMIF函数的Python列表或等效的NumPy?

时间:2018-08-01 21:11:31

标签: python arrays list numpy

我有一个二维数组:

expenses = np.array([['jim', 'sam', 'bill', 'sam'],[1,2,6,5]])

我想知道一个新数组中每个不重复人员的总支出,而无需对任何名称进行硬编码(实际列表很长),这样我就得到了这样的输出:

totals = [['jim', 'sam', 'bill'],[1,7,6]]

有没有办法使用列表或NumPy?我不想为此使用熊猫。

谢谢!

2 个答案:

答案 0 :(得分:4)

names = np.asarray(['jim', 'sam', 'bill', 'sam'])
values = np.asarray([1, 2, 6, 5])
result = {name: values[names == name].sum() for name in np.unique(names)}

答案 1 :(得分:0)

另一种有趣的方式(没有numpy)是使用Counter

from collections import Counter
names = ['jim', 'sam', 'bill', 'sam']
counts = [1,2,6,5]
c = Counter()
for name, count in zip(names,counts):
    c[name] += count
# Remapping of dict to list of lists
list(map(list, zip(*c.items())))

输出:

[['sam', 'jim', 'bill'], [7, 1, 6]]