Search code examples
pythonpandasdataframeindexingmultiplication

How to multiply all elements in a row with corresponding element in same row in different column in pandas dataframe in Python?


I have a dataframe which looks as follows: enter image description here

I want to multiply elements in a row except for the "depreciation_rate" column with the value in the same row in the "depreciation_rate" column.

I tried df2.iloc[:,6:26]*df2["depreciation_rate"] as well as df2.iloc[:,6:26].mul(df2["depreciation_rate"])

I get the same results with both which look as follows. I get NaN values with additional columns which I don't want. I think the elements in rows also multiply with values in other rows in the "depreciation_rate" column. What would be a good way to solve this issue?

enter image description here


Solution

  • Try using mul() along axis=0:

    df2.iloc[:,6:26].mul(df2["depreciation_rate"], axis=0)