【问题标题】:How to multiply all elements in a row with corresponding element in same row in different column in pandas dataframe in Python?python - 如何将一行中的所有元素与Python中pandas数据框中不同列的同一行中的相应元素相乘?
【发布时间】:2021-07-21 15:00:48
【问题描述】:

我有一个如下所示的数据框:

我想将除“depreciation_rate”列之外的行中的元素与“depreciation_rate”列中同一行中的值相乘。

我试过df2.iloc[:,6:26]*df2["depreciation_rate"]df2.iloc[:,6:26].mul(df2["depreciation_rate"])

我得到了相同的结果,如下所示。我得到 NaN 值以及我不想要的其他列。我认为行中的元素也与“depreciation_rate”列中其他行的值相乘。解决这个问题的好方法是什么?

【问题讨论】:

    标签: python pandas dataframe indexing multiplication


    【解决方案1】:

    尝试使用mul()axis=0

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

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      相关资源
      最近更新 更多