'Trying to group and find margins of pandas dataframe based on multiple columns. Keep getting IndexError

I am trying to calculate the margins between two values based on 2 other columns.

def calcMargin(data):
    marginsData = data[data.groupby('ID')['Status'].transform(lambda x: all(x != 'Tie'))] # Taking out all inquiries with ties.

    def difference(df): # Subtracts 'Accepted' from lowest price
         if len(df) <=1:
            return pd.NA
         winner = df.loc[(df['Status'] == 'Accepted'), 'Price']
         df = df[df.Status != 'Accepted']
         return min(df['Price']) - winner

    winningMargins = marginsData.groupby('ID').agg(difference(marginsData)).dropna()

    winningMargins.columns = ['Margin']

    winners = marginsData.loc[(marginsData.Status == 'Accepted'), :]
    winners = winners.join(winningMargins, on = 'ID')

    winnersMargins = winners[['Name', 'Margin']].groupby('Name').sum().reset_index()

To explain a bit further, I am trying to find the difference between two prices. One of them is wherever the "Accepted" value is in the second column. The other price is whatever is the lowest price after the "Accepted" row is extracted, then taking the difference between the two. But this is based on grouping by a third column, the ID column. Then, trying to attach the margin to the winner, 'Name', in the fourth column.

I keep getting the error -- IndexError: index 25 is out of bounds for axis 0 with size 0. Not 100% sure how to fix this, or if my code is correct.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source