0

I have a dataframe with columns like this ("item","Quantity","unit Price","Total Price") I want to split this data frames into sub data frames where the cumulative sum of total price for each small data frame is equal to or greater than 1000$ , the problem is there are items has Total Price of value greater than 1000 so we need to take quantity from this item untill 1000 and the rest will add to the next group and so on. How can I solve this problem ? I tried the code belwo but I got error

import pandas as pd

# Assume your dataframe is called "df"

df = df.sort_values(by='total price', ascending=False) # sort the dataframe by total price in descending order

# Create an empty list to store the groups

groups = []

# Initialize a variable to track the cumulative total price

cumulative_total_price = 0

# Initialize a list to store the current group

current_group = pd.DataFrame(columns=["item", "quantity", "unit price", "total price"])

# Iterate through each row in the dataframe

for index, row in df.iterrows():

    if cumulative_total_price + row['total price'] > 1000:

        # If adding the current row's total price to the cumulative total price exceeds 1000,

        # find the quantity that doesn't exceed the cumulative total price

        remaining_price = 1000 - cumulative_total_price

        remaining_quantity = int(remaining_price / row['unit price'])

        # add the remaining quantity of the item to the current group

        current_group = current_group.append({'item': row['item'], 'quantity': remaining_quantity, 'unit price': row['unit price'], 'total price': remaining_quantity * row['unit price']}, ignore_index=True)

        groups.append(current_group)

        # update the cumulative total price

        cumulative_total_price = 0

        # reset the current group

        current_group = pd.DataFrame(columns=["item", "quantity", "unit price", "total price"])

        # add the remaining quantity of the item to the next group

        current_group = current_group.append({'item': row['item'], 'quantity': row['quantity'] - remaining_quantity, 'unit price': row['unit price'], 'total price': (row['quantity'] - remaining_quantity) * row['unit price']}, ignore_index=True)

        cumulative_total_price = (row['quantity'] - remaining_quantity) * row['unit price']

    else:

        # If adding the current row's total price to the cumulative total price does not exceed 1000,

        # add the row to the current group and update the cumulative total price

        current_group = current_group.append({'item': row['item'], 'quantity': row['quantity'], 'unit price': row['unit price'], 'total price': row['total price']}, ignore_index=True)

        cumulative_total_price += row['total price']

# Add the last group to the list of groups

groups.append(current_group)
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – It_is_Chris Jan 25 '23 at 20:25
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 26 '23 at 01:54

0 Answers0