Find combination of columns summing to specific value

@shubh and here is an equivalent python script borrowing from your original post and making it work with the KNIME input table.

image

import itertools

# Copy input to output
output_table_1 = input_table_1.copy()
flow_variables['knime.workspace']

numbers = output_table_1.filter(regex="cat.*_Value|Target_Value*", axis=1)

"""
 Assumes that the "Target_Value" column will be the first column 
 in the "numbers" dataframe.
 
 So it finds all combinations of cols from second column onwards 
 that sum to equal the first column

"""

df=numbers.apply(lambda row: 
      [s for i in range(len(row)) 
        for s in itertools.combinations(row[1:], i) 
      	if sum(s)==row[0] ]
	 , axis=1) 

"""
 Creates a new column called "sets" to contain the sets created as a dataframe above
 and sets its type as a string.
"""
output_table_1["sets"]=df
output_table_1.sets=output_table_1.sets.astype("string")

This is (not surprisingly) significantly faster than my “standard nodes” offering.

Column Combinations - python.knwf (10.7 KB)

3 Likes