Python 使用函数进行计算
我必须使用我面临困难的函数进行计算 计算公式: 收益/损失:Python 使用函数进行计算,python,Python,我必须使用我面临困难的函数进行计算 计算公式: 收益/损失:(价值价格)*股票 年度收入/损失%=((价值价格)/价格)/(当前日期-购买日期))*100 stocks = [ { 'symbol': 'GOOGLE', 'share': 125, "price": 772.88, 'value': 941.53,'Purchase Date':"8-1-2015" }, { 'symbol': 'MSFT', 'share': 85, "price": 56.60, 'valu
(价值价格)*股票
年度收入/损失%=((价值价格)/价格)/(当前日期-购买日期))*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"]}')
我假设数据中的
日期
表示购买日期
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
你的问题是什么?我必须写一个函数来计算两件事:收益/损失和年收益/损失,单位为%。我只需要打印符号、份额、收益/损失和年收益/损失%,但这也不是问题。一个问题以一个问号结束。我需要一个关于如何编写计算这两个元素的函数的解决方案。你为什么需要
apply
?你在说什么apply?很抱歉,我是python新手,遇到了一些困难。@YaswanthBalusu apply是一个函数@galaxyan,但我没有在股票中使用apply:earn=stock['share']*stock['value']-stock['share']]*stock['price']print(f'{stock['symbol']}\t\t{stock['share]}\t{round(earn,2)}\t\t{stock['Date})