【问题标题】:multiplication of specific column in data frame [duplicate]数据框中特定列的乘法[重复]
【发布时间】:2019-03-26 21:33:24
【问题描述】:

我有两个数据框,即共享和 Log_Returns。我想将第一个数字与 Log_Returns 的第一列相乘。第 2 列的份额的第 2 位,依此类推。

我试过for循环,但没有输出,我也有错误。

我试过这段代码,但没有输出

shares
Out[34]: 
           0
0 -10.466597
1  92.589647
2  17.876951

Log_Return
Out[35]: 
                 UBL      WAHN       SCL
Date                                    
2018-12-26 -0.016651  0.000000  0.000000
2018-12-27 -0.022567 -0.014917  0.045282
2018-12-28 -0.034484  0.000000  0.000000
2018-12-31 -0.044806  0.000000 -0.048742


i=0
        for j in range(Log_Return.shape[1]):
#j chooses the column of Log data frame
#shape[1] gives number of columns
        for k in range(len(Log_Return)):
# k chooses the rows one by one of jth column

             shares.iloc[i,0]*Log_Return.iloc[j,k]
#you can multiply and even store the values or do any operations you want 
        i+=1
#i determines the row of shares data frame

但我收到此错误

File "<ipython-input-36-ea1f8caa95e5>", line 2
    for j in range(Log_Return.shape[1]):
    ^
IndentationError: unexpected indent

【问题讨论】:

  • 删除for关键字前的尾随空格。
  • 发生错误是因为您有一个不应缩进的缩进 for 块。 @botchniaque,这是一个挑剔的,但尾随空格是空格 after 的东西。 前导空格在事物之前

标签: python python-3.x python-2.7 dataframe python-responses


【解决方案1】:

正确缩进,这样解释器就知道你有嵌套循环。 另外,您可能想print 出产品。

        for j in range(Log_Return.shape[1]):
            i = j
            for k in range(len(Log_Return)):
                print(shares.iloc[i, 0] * Log_Return.iloc[j, k])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2023-03-20
    • 2018-03-25
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多