JoshBob
JoshBob

Reputation: 81

Weighted average on pandas

I want to add a new column which is a weighted average of the 2. and 3. columns.

  1. column * 0.6 + 3.column*0.4, the issue is that the columns change weekly so I can't just use the column names, I want it to be the 2. and 3. columns every week.

I heard that df.iloc is the way to go, but I am not sure how to apply it on this problem.

Upvotes: 1

Views: 60

Answers (1)

jezrael
jezrael

Reputation: 862481

Python counts from 0, so for select second cand third column use 1,2 in DataFrame.iloc:

df.iloc[:, 1] * 0.6 + df.iloc[:, 2] * 0.4

Upvotes: 1

Related Questions