title: Python Pandas: How do I sumproduct by rows with an if condition?
body: <p>I know there are some questions on Stack Overflow on Sumproduct but the solution are not working for me. I am also new to Python Pandas.</p>
  <p>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:</p>
  <p><strong>IndexError: ('index 2018 is out of bounds for axis 0 with size 27', 'occurred at index 0')</strong></p>
  <p>Pls help. Thank you</p>
  <pre><code># 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)
  </code></pre>
  <p><strong>df_copy</strong></p>
  <pre><code>**|2020 | 2018 | 2019 | sumpr|**
  |0    | 100  | 20   | 0    |
  |1    | 30   | 10   | 270  |
  |3    | 10   | 10   | 150  |
  </code></pre>
  <p>I am sorry if the table doesn't look like a table. I can't create a table properly in Stackoverflow.</p>
  <p>Basically <strong>for a particular row</strong>, if</p>
  <pre><code>2020 = 2 ,
  2018 =30 ,
  2019 =10 ,
  
  sumpr= 30 * 9 + 10*9 = 270
  </code></pre>