Python Forum

Full Version: Python Pandas: How do I sumproduct by rows with an if condition?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to Python Pandas.

For each row, I want to do a sumproduct of certain columns only if column['2020'] !=0. I used the below code, but get error:

IndexError: ('index 2018 is out of bounds for axis 0 with size 27', 'occurred at index 0')

Pls help. Thank you
# df_copy is my dataframe

column_list=[2018,2019]

weights=[6,9]

def test(df_copy):


    if df_copy[2020]!=0:

        W_Avg=sum(df_copy[column_list]*weights)

    else:

        W_Avg=0


    return W_Avg

 
df_copy['sumpr']=df_copy.apply(test, axis=1)
df_copy

**|2020 | 2018 | 2019 | sumpr|**
|0 | 100 | 20 | 0 |
|1 | 30 | 10 | 270 |
|3 | 10 | 10 | 150 |
I am sorry if the table doesn't look like a table. I can't create a table properly here.

Basically for a particular row, if

2020 = 2 ,
2018 =30 ,
2019 =10 ,

sumpr= 30 * 9 + 10*9 = 270
If 2018 and 2019 are column headers, it might be a good idea to
represent them as strings. Otherwise index [2018] is out of bounds.

Paul
Agree and to expand on that - any of your column references need to be in quotes. So you need to fix lines 3 and 10.

Line 12 will be problematic. I don't think it will do what you want but don't have a handy dataframe to test on at the moment. I would do as
df['w_avg'] = df[['2018','2019'].mul(weights).sum(1)
This gives a column of the sum of the weighted numbers. Which, btw, is not an average of course, but a weighted sum.