我必须使用遇到困难的功能进行计算。
计算公式:
收益/损失:(value-price)*share
%= ((value-price)/price)/(current date- purchase date))*100
的年收入/亏损
stocks = [
{ 'symbol': 'GOOGLE', 'share': 125, "price": 772.88, 'value': 941.53,'Purchase Date':"8-1-2015" },
{ 'symbol': 'MSFT', 'share': 85, "price": 56.60, 'value': 73.04 ,'Date':"8-1-2015"},
{ 'symbol': 'RDS-A', 'share': 400, "price": 49.58, 'value': 55.74 ,'Purchase Date':"8-1-2015"},
{ 'symbol': 'AIG', 'share': 235, "price": 54.21, 'value': 65.27 ,'Date':"8-1-2015"},
{ 'symbol': 'FB', 'share': 150, "price": 124.31, 'value':172.45,'Purchase Date':"8-1-2015" },
{ 'symbol': 'M', 'share': 425, "price": 30.30, 'value': 23.98 ,'Purchase Date':"1-10-2017"},
{ 'symbol': 'F', 'share': 85, "price": 150.37, 'value': 145.30,'Date':"2-17-2017" },
{ 'symbol': 'IBM', 'share': 80, "price": 150.37, 'value': 145.30,'Purchase Date':"5-12-2017"}
]
print('Stock ownership for Bob Smith')
print('-----------------------------------------')
print('Stock Share # Earn/Lose Date')
print('----------------------------------------')
for stock in stocks:
earn = stock['share'] * stock['value'] - stock['share'] * stock['price']
print(f'{stock["symbol"]}\t\t{stock["share"]}\t\t{round(earn, 2)}\t\t{stock["Date"]}')
答案 0 :(得分:0)
我认为您数据中的Date
意味着Purchase Date
stocks = [
{ 'symbol': 'GOOGLE', 'share': 125, "price": 772.88, 'value': 941.53,'Purchase Date':"8-1-2015" },
{ 'symbol': 'MSFT', 'share': 85, "price": 56.60, 'value': 73.04 ,'Purchase Date':"8-1-2015"},
{ 'symbol': 'RDS-A', 'share': 400, "price": 49.58, 'value': 55.74 ,'Purchase Date':"8-1-2015"},
{ 'symbol': 'AIG', 'share': 235, "price": 54.21, 'value': 65.27 ,'Purchase Date':"8-1-2015"},
{ 'symbol': 'FB', 'share': 150, "price": 124.31, 'value':172.45,'Purchase Date':"8-1-2015" },
{ 'symbol': 'M', 'share': 425, "price": 30.30, 'value': 23.98 ,'Purchase Date':"1-10-2017"},
{ 'symbol': 'F', 'share': 85, "price": 150.37, 'value': 145.30,'Purchase Date':"2-17-2017" },
{ 'symbol': 'IBM', 'share': 80, "price": 150.37, 'value': 145.30,'Purchase Date':"5-12-2017"}
]
df['Yearly Earning/Loss in %'] = 100*(df.value-df.price)/df.price/(pd.to_datetime('today')-pd.to_datetime(df['Purchase Date'])).dt.days
df['Earnings/loss'] = (df.value -df.price)*df.share
Purchase Date price share symbol value Yearly Earning/Loss in % Earnings/loss
0 8-1-2015 772.88 125 GOOGLE 941.53 0.015963 21081.25
1 8-1-2015 56.60 85 MSFT 73.04 0.021248 1397.40
2 8-1-2015 49.58 400 RDS-A 55.74 0.009089 2464.00
3 8-1-2015 54.21 235 AIG 65.27 0.014925 2599.10
4 8-1-2015 124.31 150 FB 172.45 0.028329 7221.00
5 1-10-2017 30.30 425 M 23.98 -0.024861 -2686.00
6 2-17-2017 150.37 85 F 145.30 -0.004209 -430.95
7 5-12-2017 150.37 80 IBM 145.30 -0.004702 -405.60