Python Forum
Python Pandas: How do I sumproduct by rows with an if condition?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Pandas: How do I sumproduct by rows with an if condition?
#1
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
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu)
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to combine multiple rows of strings into one using pandas? shantanu97 1 422 Aug-22-2021, 05:26 AM
Last Post: klllmmm
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 460 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 579 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 2,064 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 2,020 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  Pandas, Assign a value in a row, to another column based on a condition klllmmm 6 2,038 Oct-16-2020, 04:43 PM
Last Post: klllmmm
  How to diff pandas rows and modify column value Mekala 1 643 Sep-18-2020, 12:38 PM
Last Post: Mekala
  How to mark duplicate rows in pandas Mekala 3 955 Sep-17-2020, 11:32 PM
Last Post: scidam
  else condition not called when if condition is false Sandz1286 10 2,533 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 2,923 Jun-01-2020, 06:00 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020