Home > Software engineering >  Dividing each element of a python dataframe to a Series
Dividing each element of a python dataframe to a Series

Time:05-06

I have a dataframe like below:

import pandas as pd

data1 = {"a":[1.,3.,5.,2.],
     "b":[4.,8.,3.,7.],
     "c":[5.,45.,67.,34]}
data2 = {"a":[4., 6, 8]
     }

df = pd.DataFrame(data1)
df2 = pd.DataFrame(data2) 

So each element of df should be divided by the first row of df2 until all.

What I did:

df = df.divide(df2, axis=0 )

It gives me all the rows as NAN. Can you tell me what is wrong?

expected output:

       a         b       c
0  0.250  1.000000   1.250
1  0.500  1.333333   7.500
2  0.625  0.375000   8.375
3  2.000  7.000000  34.000

CodePudding user response:

The exact output that you expect is unclear, but if I guess correctly, you might want to only divide the common indices/columns and leave the rest untouched.

For this you can use combine_first to fill the NaNs after the division:

column "a" only:

df.div(df2, axis=0).combine_first(df)

all columns:

df.div(df2['a'], axis=0).combine_first(df)

or reindex_like and fillna to align the dataframes before division.

column "a" only

df.div(df2.reindex_like(df).fillna(1), axis=0)

output (column a only):

       a    b     c
0  0.250  4.0   5.0
1  0.500  8.0  45.0
2  0.625  3.0  67.0
3  2.000  7.0  34.0

output (all columns):

       a         b       c
0  0.250  1.000000   1.250
1  0.500  1.333333   7.500
2  0.625  0.375000   8.375
3  2.000  7.000000  34.000
  • Related