Home > Enterprise >  Modify data frame based on the row index - Python
Modify data frame based on the row index - Python

Time:04-13

Given a pandas dataframe (20, 40), I would like to modify the first 10 rows of the first 20 columns using the value index. For example, if:

df.iloc[5,6] = 0.98,

I would like to modify the value in the following way

new df.iloc[5,6] = 0.98 ** -(1/5)
where 5 is the row index.

And I should do the same for every value between the first 10 rows and the first 20 columns.

Can anyone help me? Thank you very much in advance.

CodePudding user response:

Can you explain what you want to do in a more general way? I don't understand why you chose 5 here.

The way to make columns off other columns is

df["new column"] = df["column1"] ** (-1/df["column2"])

the way you do it with the index is the same

    df["new column"] = df["column1"] ** (-1/df.index)

CodePudding user response:

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.

  • Related