I have the below DataFrame:
ID Start End Variance
1 100000 120000 20000
1 1 0 -1
1 7815.58 7815.58 0
1 5261 5261 0
1 138783.2 89969.37 -48813.83
1 2459.92 2459.92 0
2 101421.99 93387.45 -8034.54
2 940.04 940.04 0
2 63.06 63.06 0
2 2454.86 2454.86 0
2 830 830 0
2 299 299 0
2 14000 12000 2000
2 1500 500 1000
I want to create a new column, Overspend Total. But I only want to sum the values that are greater than 0. The resulting DataFrame will look like this:
ID Start End Variance Overspend Total
1 100000 120000 20000 20000
1 1 0 -1 20000
1 7815.58 7815.58 0 20000
1 5261 5261 0 20000
1 138783.2 89969.37 -48813.83 20000
1 2459.92 2459.92 0 20000
2 101421.99 93387.45 -8034.54 3000
2 940.04 940.04 0 3000
2 63.06 63.06 0 3000
2 2454.86 2454.86 0 3000
2 830 830 0 3000
2 299 299 0 3000
2 14000 12000 2000 3000
2 1500 500 1000 3000
I tried the following
df['Overspend Variance'] = df[df['Variance'] > 0].groupby(df['ID']).transform('sum')
But I'm getting the below error:
ValueError: Wrong number of items passed 8, placement implies 1
I know df['Overspend Variance'] = df['Variance'].groupby(df['ID']).transform('sum') would work without a condition, but I can't figure out how to incorporate it with an extra condition.