Skip to content
Advertisement

creating pandas function equivalent for EXCEL OFFSET function

Let’s say input was

JavaScript

I want to create an additional column that looks like this:

JavaScript

Basically each number in offset is telling you the number of columns to sum over (from col1 as ref point).

Is there a vectorized way to do this without iterating through each value in offset?

Advertisement

Answer

You use np.select. To use it, create each of the column sum (1, 2, 3 … as needed) as the possible choices, and create a boolean masks for each value in offset column as the possible conditons.

JavaScript

Note: this assumes that your offset column is the last one

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement