简体   繁体   中英

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: 在此处输入图像描述

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?

在此处输入图像描述

Try using mul() along axis=0 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM