【问题标题】:Find rows in which sum of column values have a specific result查找列值总和具有特定结果的行
【发布时间】:2021-02-11 02:36:42
【问题描述】:

我有以下数据框:

     value
0        2
1        3
2       10
3        1
4       12

我需要建立一个公式来识别哪些行,当值相加时,结果是 23。

在这种情况下,输出应该类似于[2,3,4] (10+1+12)。 我相信这是排列/组合字段中的东西,但是the option I found 使我更接近该目标需要特定长度的组合,但事实并非如此,因为组合可能由 n 个值组成(我永远不会预先知道 n 的确切大小)。

有没有办法做到这一点?

【问题讨论】:

  • 这不是subset sum吗?
  • 似乎确实是这样的东西,某种求解器

标签: python pandas combinations subset-sum


【解决方案1】:
from pandas import Series
import itertools

s = Series([2, 3, 10, 1, 12])

result = []
for a, b, c in itertools.combinations(s.index, 3):
    combination_sum = s.iloc[[a, b, c]].sum()
    
    if combination_sum == 23:
        result.append((a, b, c))

result

您可以将其概括为 n 个值的函数。

这就是你将如何概括它

在示例系列中,我添加了更多值以便更好地理解

from pandas import Series
import itertools

s = Series([2, 3, 10, 1, 12, 4, 5, 6, 7, 8])

def get_column_whose_sum_is(sum_value=23, combination_of_columns=3, data_as_series=s):
    result = []
    for columns in itertools.combinations(data_as_series.index, combination_of_columns):
        combination_sum = data_as_series.iloc[list(columns)].sum()

        if combination_sum == sum_value:
            result.append(columns)
            
    return result

get_column_whose_sum_is(sum_value = 33, combination_of_columns = 4, data_as_series = s) 
#  [(1, 2, 4, 9), (2, 4, 5, 8), (2, 4, 6, 7), (4, 7, 8, 9)]

get_column_whose_sum_is(sum_value = 23, combination_of_columns = 3, data_as_series = s) 
# [(1, 4, 9), (2, 3, 4), (2, 6, 9), (2, 7, 8), (4, 5, 8), (4, 6, 7)]

#for loop to find all combinations possibilities

c=[]
for i in range(len(s.index)):
    c=c+get_column_whose_sum_is(sum_value = 23, combination_of_columns = i, data_as_series = s)

print(c)

#[(1, 4, 9), (2, 3, 4), (2, 6, 9), (2, 7, 8), (4, 5, 8), (4, 6, 7), (0, 1, 2, 9), (0, 1, 4, 7), (0, 2, 5, 8), (0, 2, 6, 7), (0, 3, 4, 9), (0, 4, 5, 6), (0, 7, 8, 9), (1, 2, 5, 7), (1, 3, 4, 8), (1, 6, 8, 9), (2, 3, 5, 9), (2, 3, 6, 8), (3, 4, 5, 7), (5, 6, 7, 9), (0, 1, 2, 3, 8), (0, 1, 3, 4, 6), (0, 1, 5, 7, 9), (0, 1, 6, 7, 8), (0, 2, 3, 5, 7), (0, 3, 6, 8, 9), (1, 2, 3, 5, 6), (1, 3, 5, 8, 9), (1, 3, 6, 7, 9), (3, 5, 6, 7, 8), (0, 1, 3, 5, 6, 9), (0, 1, 3, 5, 7, 8)]


请注意,即使是小样本,subset sum 也可能会导致性能问题。

【讨论】:

  • 我如何概括它?我永远无法确定 n 的大小
  • 我已经根据您的要求概括了所有内容。希望这会有所帮助:)
  • 谢谢!这确实更清楚。据我了解,下一步是将其放入在不同的combination_of_columns 上运行的for 循环中,对吧?
  • 我不确定确切的尺寸,但我会确定最大尺寸
  • 好的,我过几分钟试试
猜你喜欢
  • 2013-12-06
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多