You can do this operation in-place with the following snippet.
from numpy.random import default_rng
from pandas import DataFrame
from string import ascii_lowercase
rng = default_rng(0)
df = DataFrame(
(data := rng.integers(1, 10, size=(4, 5))),
columns=[*ascii_lowercase[:data.shape[1]]]
)
print(df)
a b c d e
0 8 6 5 3 3
1 1 1 1 2 8
2 6 9 5 6 9
3 7 6 5 6 9
# you would replace :3, :4 with :10, :20 for your data
df.iloc[:3, :4] **= (-1 / df.index)
print(df)
a b c d e
0 0 0.166667 0.447214 0.693361 3
1 1 1.000000 1.000000 0.793701 8
2 0 0.111111 0.447214 0.550321 9
3 7 6.000000 5.000000 6.000000 9
In the event your index is not a simple RangeIndex
you can use numpy.arange
to mimic this:
from numpy import arange
df.iloc[:3, :4] **= (-1 / arange(df.shape[0]))
print(df)
a b c d e
0 0 0.166667 0.447214 0.693361 3
1 1 1.000000 1.000000 0.793701 8
2 0 0.111111 0.447214 0.550321 9
3 7 6.000000 5.000000 6.000000 9
Note: If 0 is in your index, like it is in this example, you'll encounter a RuntimeWarning of dividing by 0.