Python Forum
Python Pandas: How do I sumproduct by rows with an if condition? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Pandas: How do I sumproduct by rows with an if condition? (/thread-34268.html)



Python Pandas: How do I sumproduct by rows with an if condition? - JaneTan - Jul-13-2021

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


RE: Python Pandas: How do I sumproduct by rows with an if condition? - DPaul - Jul-13-2021

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


RE: Python Pandas: How do I sumproduct by rows with an if condition? - jefsummers - Jul-13-2021

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.