Question: I am trying to figure out how to create the equivalent of SUMIF in Python. The solution I have currently works, but it is way too inefficient and it takes 20 minutes to run. What would be the most efficient way to reach the result that i want? Here is the what I am doing currently boiled down to a very simple form. In the "real" code there are many more conditions. **sales_data customer_1** Transactions | Product Dimension 4 | Product Dimension 2 | Product Dimension 3 | sum_of_sales -------------- | ------------------- | ------------------- | --------------------| ------------- 1 | 50 | F80 | ETQ546 | 80 2 | 50 | F80 | SAS978 | 20 3 | 50 | C36 | JBH148 | 10 4 | 50 | F80 | ETQ546 | 80 5 | 50 | F80 | SAS978 | 20 6 | 50 | C36 | JBH148 | 10 7 | 20 | A20 | OPW269 | 15 8 | 20 | A20 | DUW987 | 65 9 | 20 | v90 | OWQ897 | 47 **condition_types BEFORE ADDING SUMIF TO TABLE** Transactions | Type | Product Dimensions | -------------- | ------------------- | ------------------- | customer_1 | ABC | 50 | customer_1 | DEF | F80 | customer_1 | GHI | JBH148 | **condition_types AFTER ADDING SUMIF TO TABLE** Transactions | Type | Product Dimensions | sum_of_sales -------------- | ------------------- | ------------------- | ------------- customer_1 | ABC | 50 | 220 customer_1 | DEF | F80 | 200 customer_1 | GHI | JBH148 | 20  Define the sumif function def sumif(row, value_column): if row['Type'] == "ABC": filtered_data = sales_data.loc[ (sales_data['Product_dimension_4'] == row['Product Dimensions']) ] elif row['Type'] == "DEF" and row['Product Dimensions'] in sales_data['Product_dimension_2'].unique(): filtered_data = sales_data.loc[ (sales_data['Product_dimension_2'] == row['Product Dimensions']) ] elif row['Type'] == "GHI" and row['Product Dimensions'] in sales_data['Product_dimension_3'].unique(): filtered_data = sales_data.loc[ (sales_data['Product_dimension_3'] == row['Product Dimensions']) ] else: return 0 # Return 0 instead of an empty string for consistency return filtered_data[value_column].sum()  Apply the sumif function using loc condition_types['sum_of_sales'] = condition_types.apply(lambda row: sumif(row, value_column="sum_of_sales"), axis=1)  I hope this is clear enough and the example is not too complicated.

Answer: For a more general approach, you can melt the sales_data to get a relationship between 'Product Dimensions' and 'sum_of_sales', then groupby on 'Product Dimensions', aggregate with sum, and merge it on conditions_data: sales_data = ( pd.melt( sales_data, id_vars=["sum_of_sales"], value_vars=sales_data.filter(like="Product Dimension").columns, value_name="Product Dimensions", ) .groupby("Product Dimensions", as_index=False)["sum_of_sales"] .sum() ) condition_types = condition_types.merge(sales_data, how="left")   Transactions Type Product Dimensions sum_of_sales 0 customer_1 ABC 50 220 1 customer_1 DEF F80 200 2 customer_1 GHI JBH148 20   But if each 'Type' must respect a relationship with a specific 'Product Dimension' column, you can create a mapping and also use 'Type' to group and merge: m = { "Product Dimension 4": "ABC", "Product Dimension 2": "DEF", "Product Dimension 3": "GHI", } sales_data = pd.melt( sales_data, id_vars=["Transactions", "sum_of_sales"], value_vars=sales_data.filter(like="Product Dimension").columns, var_name="Product Dimension Type", value_name="Product Dimensions", ) sales_data["Type"] = sales_data["Product Dimension Type"].map(m) sales_data = sales_data.groupby(["Product Dimensions", "Type"], as_index=False)[ "sum_of_sales" ].sum() condition_types = condition_types.merge( sales_data, on=["Product Dimensions", "Type"], how="left" )  Output is the same for the provided data, but may differ if you have repeated values in different 'Product Dimension' columns.