【问题标题】:How to multiply between the columns of different rows in python如何在python中不同行的列之间相乘
【发布时间】:2019-02-15 16:39:10
【问题描述】:

这是等式。示例

Numbers     | Date      | Mean
1000        |12/1/2018  | 1
1002        |12/2/2018  | 0
1003        |12/3/2018  | 0.5
 0          |12/4/2018  | 0.6
 0          |12/5/2018  | 0.4
 0          |12/6/2018  |0.1
 0          |12/7/2018  | -0.7
 0          |12/8/2018  | 0.2
 0          |12/9/2018  | -0.1

这里有我想要的

|Numbers | Date      | Mean | Multiplication |
| ------ |-----------|------|----------------|
|1000    | 12/1/2018 | 1    | 1000           |
|1002    | 12/2/2018 | 0    | 0*1000= 0      |
|1003    | 12/3/2018 | 0.5  | 0.5*1002=501   |
|0       | 12/4/2018 | 0.6  | 1003*0.6=601.8 |
|0       | 12/5/208  | 0.4  | 601.8*0.4
|0       | 12/6/2018 | 0.1  | 601.8*0.4*0.1  |
|0       |12/7/2018  | -0.7 |601.8*0.4*0.1*-0.7| 
 0       |12/8/2018  | 0.2  |601.8*0.4*0.1*-0.7*0.2
 0       |12/9/2018  | -0.1 |601.8*0.4*0.1*-0.7*0.2*-0.1

数据已经在数据框中,我正在使用 pandas 函数

【问题讨论】:

  • 你的标题和你的最终输出不清楚。两者相互矛盾。请重新检查,然后重新发布。
  • @Parth Dhir,你之前的标题更有意义,你说“如何将第二行的第三列与第一行的第一列相乘。你不能将 1000 作为值对于第一行列乘法。它打破了模式。
  • @Parth Dhir,您改变了问题的基本概念。完成后,我将删除我的答案并发布。
  • @Raj006 抱歉给您带来不便
  • @Parth Dhir,您需要用文字解释您要为乘法列实现的目标。我开始为您更改的问题创建答案,而您再次更改了要求。请在明确您想要的内容后编辑帖子。

标签: python python-3.x python-2.7 jupyter-notebook data-science


【解决方案1】:

@Daniel Labbe 回答了最初的要求,而且是正确的。 +1 给他 shift() 方法。然后,用户需求发生了变化。所以,这是我对最新要求的回答。

#import pandas for managing data with dataframe
import pandas as pd
#import tabulate to print your data frame as table
from tabulate import tabulate
#Create a data dictionary
myData={'Numbers':[1000,1002,1003,0,0,0,0,0,0],'Date':['12/1/2018','12/2/2018','12/3/2018','12/4/2018','12/5/2018','12/6/2018','12/7/2018','12/8/2018','12/9/2018'],'Mean':[1,0,0.5,0.6,0.4,0.1,-0.7,0.2,-0.1]}
#Create a data frame from the data dictionary using pandas. User mentioned that the data is already in the
#pandas data frame
myDataFrame=pd.DataFrame(myData)
#Print your final table (just pretty print)
print(tabulate(myDataFrame, headers='keys', tablefmt='psql'))
#Declare a list
MultiplicationList=[]
#Declare a constant
StorePreviousValue=0
for i in range(0,len(myDataFrame['Numbers'])):
    #If it is the first row then use the Number
    if i==0:
        #Append the value to the list
        MultiplicationList.append(myDataFrame['Numbers'][i])
    else:
        #If it is not the first row, and the value in the first column of the previous row is '0'
        #multiply Mean with the previous multiplication result
        if myDataFrame['Numbers'][i-1]==0:
            StorePreviousValue=StorePreviousValue*myDataFrame['Mean'][i]
        #If it is not the first row, and the value in the first column of the previous row is not '0'
        #(should probably say greate than '0', but the question is not clear about that), then 
        #multiply Mean with the Number in the first column of the previous row
        else:
            StorePreviousValue=myDataFrame['Numbers'][i-1]*myDataFrame['Mean'][i]
        #Append the value to the list
        MultiplicationList.append(StorePreviousValue)
#Create a new column in the data frame and pass the list as the value
myDataFrame['Multiplication']=MultiplicationList
#Print your final table (just pretty print)
print(tabulate(myDataFrame, headers='keys', tablefmt='psql'))

这是输出

+----+-----------+-----------+--------+
|    |   Numbers | Date      |   Mean |
|----+-----------+-----------+--------|
|  0 |      1000 | 12/1/2018 |    1   |
|  1 |      1002 | 12/2/2018 |    0   |
|  2 |      1003 | 12/3/2018 |    0.5 |
|  3 |         0 | 12/4/2018 |    0.6 |
|  4 |         0 | 12/5/2018 |    0.4 |
|  5 |         0 | 12/6/2018 |    0.1 |
|  6 |         0 | 12/7/2018 |   -0.7 |
|  7 |         0 | 12/8/2018 |    0.2 |
|  8 |         0 | 12/9/2018 |   -0.1 |
+----+-----------+-----------+--------+
+----+-----------+-----------+--------+------------------+
|    |   Numbers | Date      |   Mean |   Multiplication |
|----+-----------+-----------+--------+------------------|
|  0 |      1000 | 12/1/2018 |    1   |      1000        |
|  1 |      1002 | 12/2/2018 |    0   |         0        |
|  2 |      1003 | 12/3/2018 |    0.5 |       501        |
|  3 |         0 | 12/4/2018 |    0.6 |       601.8      |
|  4 |         0 | 12/5/2018 |    0.4 |       240.72     |
|  5 |         0 | 12/6/2018 |    0.1 |        24.072    |
|  6 |         0 | 12/7/2018 |   -0.7 |       -16.8504   |
|  7 |         0 | 12/8/2018 |    0.2 |        -3.37008  |
|  8 |         0 | 12/9/2018 |   -0.1 |         0.337008 |
+----+-----------+-----------+--------+------------------+

如果您没有 pandas 或制表,请使用安装 点安装熊猫 点安装表格

如果您不熟悉 pip,请 google 一下。 这个答案假设您知道如何从文件中读取并创建数据字典。如果你不这样做,那将是另一个问题。

【讨论】:

  • 您已经使用字典 mydata 完成了 for 循环。如果我们在 myInitialDataFrame 上执行 for 循环怎么办?
  • 感谢您的帮助。对不起,我一次又一次地编辑我的要求。只是想学习的东西。让我们假设下一行的平均值是-0.7。那还需要补充什么?
  • @ParthDhir,添加了更多您更新问题的数据。
  • 如果我想使用 iloc 函数而不是 loc 怎么办?我在使用 loc 函数时遇到问题
【解决方案2】:

如果您使用 Pandas 数据框,则可以使用 shift() 方法:

df['Multiplication'] = df.Mean * df.Numbers.shift(1)
df.loc[0, 'Multiplication'] = df.Numbers[0]
for i in range(len(df[df.Numbers.shift(1) == 0])):
    df.loc[df[df.Numbers.shift(1) == 0].index, 'Multiplication'] = df[df.Numbers.shift(1) == 0].Mean.values * df[df.index.isin(df[df.Numbers.shift(1) == 0].index-1)].Multiplication.values

输出如下:

第一行你没有两个数字相乘,所以在相乘之后更新值。

现在满足零数值的要求。

按照@Raj006 的建议,稍微分解一下代码:

# return just the rows that match the condition (Numbers column 0 for the row before)
df[df.Numbers.shift(1) == 0].index

# update the values for the column Multiplication with the matching rows
df.loc[df[df.Numbers.shift(1) == 0].index, 'Multiplication']

# the value to be update is the [Mean value for the matching rows] * [rows before the matching Multiplication value]
df[df.Numbers.shift(1) == 0].Mean.values * df[df.index.isin(df[df.Numbers.shift(1) == 0].index-1)].Multiplication.values

编辑: 不幸的是,一旦计算取决于之前的计算,我不得不使用 for 循环来运行与匹配行一样多的次数。

【讨论】:

  • 感谢您的帮助。对不起,我一次又一次地编辑我的要求。只是想学习的东西。让我们假设下一行的平均值是-0.7。那么代码中需要添加什么?
  • 代码应该是通用的,并且无论您考虑约束的数据如何都可以工作。
  • 如果我乘以 420 而是乘法列的最后一行,平均值为 -0.7。你认为结果应该是什么? -294?如果 420 是美元金额而不仅仅是数字呢?
  • 请将数据添加到问题中,以便我进行测试。
  • @ParthDhir 新数据集的最后三个值是 -16.850400、-3.370080 和 0.337008。正是您所期望的,考虑到:-16.8504=601.8*0.4*0.1*-0.7 -3.37008=601.8*0.4*0.1*-0.7*0.2 0.337008=601.8*0.4*0.1*-0.7*0.2*-0.1
猜你喜欢
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2021-07-21
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多