【发布时间】:2019-12-15 20:36:08
【问题描述】:
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
我能理解的简单答案优于我不能理解的复杂答案。感谢您的帮助。
【问题讨论】:
标签: python pandas numpy dataframe reshape