Python Pandas: divide a Series by a Dataframe

Question:

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.

Asked By: Jingwei Yu

||

Answers:

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
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.