Home > other >  Python Pandas: divide a Series by a Dataframe
Python Pandas: divide a Series by a Dataframe

Time:08-09

I have a Series and a Dataframe that share the same index:

s = pd.Series([300, 300])
df = pd.DataFrame({
    'A': [10,20],
    'B': [20,30]
})

When I do s.div(df), I see:

     A    B    0    1
0  NaN  NaN  NaN  NaN
1  NaN  NaN  NaN  NaN

I expect:

     A    B
0   30   15
1   15   10

pandas.__version__: 1.3.4.

CodePudding user response:

Use DataFrame.rdiv for divide from right side:

df1 = df.rdiv(s, axis=0)
print (df1)
      A     B
0  30.0  15.0
1  15.0  10.0
  • Related