将数据框单元格乘以矩阵值时出现问题

时间:2019-12-15 20:36:08

标签: python pandas numpy dataframe reshape

在数据框中具有简单组合的Noob(试图学习data_science)。我想卖出每家公司一定数量的股票,将出售的股票数量乘以价格,然后将其乘以现有现金价值(15000),四舍五入到小数点后两位。简短

new_port_df =
               Name   Price   Starting Number_of_shares
           0   MMM    10.00   50
           1   AXP    20.00   100
           2   AAPL   30.00   1000 
           3   Cash    1.00   15000

 shares_sold = [[ 5.] [ 15.] [75.] [   0.]] #(numpy.ndarray, shape (4,1))

 new_port_df['Price'] = 

           0    10.00
           1    20.00
           2    30.00
           3     1.00
           Name: Low, dtype: float64 # pandas.core.series.Series

基本上就是现金+ = 5 * 10 + 15 * 20 + 75 * 30 + 0 * 1或15000 + 2600 = 17600

作为中间步骤(在谷歌搜索并阅读此处的其他文章之后),我尝试过:

cash_proceeds = np.dot(shares_sold, new_port['Price'])

ValueError: shapes (4,1) and (4,) not aligned: 1 (dim 1) != 4 (dim 0). I think I should be reshaping, but haven't had any luck.  

期望的结果如下(除了17600单元以外,其他所有结果都可以)

updated_port_df =
               Name   Price   Starting Number_of_shares
           0   MMM    10.00   45
           1   AXP    20.00   85
           2   AAPL   30.00   925 
           3   Cash    1.00   17600 # only the 17600 not working

我能理解的简单答案比我不能理解的复杂答案更受欢迎。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用熊猫dot代替np.dot。您需要1-d numpy数组才能在序列上使用点,因此需要将shares_sold转换为1-d

shares_sold = np.array([[ 5.], [ 15.], [75.] ,[   0.]])
shares_sold_1d = shares_sold.flatten()

cash_proceeds = new_port_df['Price'].dot(shares_sold_1d)

In [226]: print(cash_proceeds)
2600.0

要获得所需的输出,只需使用.loc赋值和减法即可

(new_port_df.loc[new_port_df.Name.eq('Cash'), 'Starting_Number_of_shares'] = 
              new_port_df.loc[new_port_df.Name.eq('Cash'), 'Starting_Number_of_shares'] 
              + cash_proceeds)

new_port_df['Starting_Number_of_shares'] = new_port_df['Starting_Number_of_shares'] - shares_sold_1d

Out[235]:
   Name  Price  Starting_Number_of_shares
0   MMM   10.0                       45.0
1   AXP   20.0                       85.0
2  AAPL   30.0                      925.0
3  Cash    1.0                    17600.0

注意:如果您确实要使用np.dot,则需要按以下步骤交换订单

In [237]: np.dot(new_port_df['Price'], shares_sold)
Out[237]: array([2600.])

答案 1 :(得分:1)

您可以仅创建一个数字列表来解决np.dot()错误,而不是将share_sold作为列表列表(即[[],[],[]])启动。

shares_sold = [5,15,75,0]
cash_proceeds = np.dot(new_port_df['Price'], shares_sold)

或正如Andy指出的,如果shares_sold已作为列表列表启动,则可以将其转换为数组,然后将其展平并从那里继续。我的回答不会解决随之而来的方法变化。

然后,您可以更改shares_sold列表/数组中的最后一项,以反映出售股票产生的现金变化(通知另存为负,因为这些将从您的“股份数量”列中减去):

shares_sold[3] = -cash_proceeds

现在,您可以从“股份数量”列中减去已售出的股票以反映更改(您指示要在updated_port_df中容纳此信息,因此我首先复制初始投资组合,然后进行更改),

updated_port_df = new_port_df.copy()
updated_port_df['Number_of_shares'] = updated_port_df['Number_of_shares'] - shares_sold