Home > Enterprise >  Apply transformation to masked dataframe
Apply transformation to masked dataframe

Time:11-10

I have this matrix df.head():

    0   1   2   3   4   5   6   7   8   9   ... 1848    1849    1850    1851    1852    1853    1854    1855    1856    1857
0   0.0 0.0 0.0 0.0 0.0 0.00000 0.0 0.0 0.0 0.00000 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   0.0 0.0 0.0 0.0 0.0 0.00000 0.0 0.0 0.0 0.00000 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2   0.0 0.0 0.0 0.0 0.0 0.00000 0.0 0.0 0.0 30.88689    ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3   0.0 0.0 0.0 0.0 0.0 0.00000 0.0 0.0 0.0 0.00000 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4   0.0 0.0 0.0 0.0 0.0 42.43819    0.0 0.0 0.0 0.00000 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5 rows × 1858 columns

And I need to apply a transformation to it every time a value other than 0.0 is found, dividing the value by 0.32

So far I have the mask, like so:

    normalize = 0.32
    mask = (df>=0.0)
    df = df.where(mask)

How do I apply such a transformation on a very large dataframe, after masking it?

CodePudding user response:

You don't need mask, just divide your dataframe by 0.32.

df / 0.32
>>> df
   A  B
0  0  3
1  5  0

>>> df / 0.32
        A      B
0   0.000  9.375
1  15.625  0.000

CodePudding user response:

If you needed to use mask, try;

mask = (df.eq(0))
df.where(mask, df/0.32)
  • Related