Python 如何使用Apply()函数解决此问题?
我有一个df,随机投资组合,如下所示 随机投资组合具有不同的权重 我也有这些行动的一年回报 现在我想在df中添加两列“收益率”和“波动率”。收益率和波动率必须与每一行的值成函数关系,并对存在的每一行进行函数关系 他试图使用Apply()函数 我要寻找的是,这个结果出现在与之对应的一行中(本例中为第0行),并对下面的行执行相同的操作。PythonPython 如何使用Apply()函数解决此问题?,python,python-3.x,pandas,numpy,apply,Python,Python 3.x,Pandas,Numpy,Apply,我有一个df,随机投资组合,如下所示 随机投资组合具有不同的权重 我也有这些行动的一年回报 现在我想在df中添加两列“收益率”和“波动率”。收益率和波动率必须与每一行的值成函数关系,并对存在的每一行进行函数关系 他试图使用Apply()函数 我要寻找的是,这个结果出现在与之对应的一行中(本例中为第0行),并对下面的行执行相同的操作。Pythondef complex_computation():表示未定义名称“Returns” 之后 及 如何正确运行代码 救救我 我的第一个回答是不使用循环,而是
def complex_computation():
表示未定义名称“Returns”
之后
及
如何正确运行代码
救救我 我的第一个回答是不使用循环,而是编写一个函数,将其应用到数据帧中。但经过一番搜索,我发现了这篇简洁的帖子:也许这会对你有所帮助。非常感谢@jho,你可以再给我解释一下我将如何完成我的功能,对不起,我几乎不是初学者。我将非常感谢您在
complex_computation
中计算收益率和波动率,这两种收益率都是您的。在func
中,您可以将这些值映射到列。谢谢@jho。抱歉,我仍然被这个问题困扰着。我是这样做的:def complex_computation():WeightedReturns=StockReturns.mul(random\u portapolios,axis=1)returnsDay=WeightedReturns.sum(axis=1)mean_Returns\u daily=np.mean(ReturnsDaily)Returns=(1+mean\u Returns\u daily)**252)-1 cov\u mat=StockReturns.cov()cov\u mat\u annual=cov\u mat*252 Volatility=np.sqrt(np.dot(random\u portapolios.T,np.dot(cov\u mat\u annual,random\u portapolios)))返回,Volatility
我的第一个回答是不使用循环,而是编写一个函数,并将其应用到数据帧。但经过一番搜索,我发现了这篇简洁的帖子:也许这会对你有所帮助。非常感谢@jho,你可以再给我解释一下我将如何完成我的功能,对不起,我几乎不是初学者。我将非常感谢您在complex_computation
中计算收益率和波动率,这两种收益率都是您的。在func
中,您可以将这些值映射到列。谢谢@jho。抱歉,我仍然被这个问题困扰着。我是这样做的:def complex_computation():WeightedReturns=StockReturns.mul(random\u portapolios,axis=1)returnsDay=WeightedReturns.sum(axis=1)mean_Returns\u daily=np.mean(ReturnsDaily)Returns=(1+mean\u Returns\u daily)**252)-1 cov\u mat=StockReturns.cov()cov_mat_annual=cov_mat*252 Volatility=np.sqrt(np.dot(random_Portapolios.T,np.dot(cov_mat_annual,random_Portapolios)))回报率,波动率
>>> random_portafolios
AAPL weight MSFT weight XOM weight JNJ weight JPM weight AMZN weight GE weight FB weight T weight
0 0.188478 0.068795 0.141632 0.147974 0.178185 0.040370 0.020516 0.047275 0.166774
1 0.236818 0.008540 0.082680 0.088380 0.453573 0.021001 0.014043 0.089811 0.005155
2 0.179750 0.071711 0.050107 0.089424 0.080108 0.106136 0.155139 0.073487 0.194138
3 0.214392 0.015681 0.034284 0.276342 0.118263 0.002101 0.057484 0.000317 0.281137
4 0.301469 0.099750 0.046454 0.093279 0.020095 0.073545 0.178752 0.146486 0.040168
5 0.132916 0.006199 0.305137 0.032262 0.090356 0.169671 0.205602 0.003686 0.054172
>>> StockReturns.head()
AAPL MSFT XOM TWTR JPM AMZN GE FB T
Date
2017-01-04 -0.001164 -0.004356 -0.011069 0.025547 0.001838 0.004657 0.000355 0.015660 -0.005874
2017-01-05 0.005108 0.000000 -0.014883 0.013642 -0.009174 0.030732 -0.005674 0.016682 -0.002686
2017-01-06 0.011146 0.008582 -0.000499 0.004681 0.000123 0.019912 0.002853 0.022707 -0.019930
2017-01-09 0.009171 -0.003170 -0.016490 0.019220 0.000741 0.001168 -0.004979 0.012074 -0.012641
2017-01-10 0.001049 -0.000335 -0.012829 -0.007429 0.002837 -0.001280 -0.002859 -0.004404 0.000278
def complex_computation():
WeightedReturns = StockReturns.mul(arr, axis=1)
ReturnsDaily= WeightedReturns.sum(axis=1)
mean_retorns_daily = np.mean(ReturnsDaily)
Returns = ((1+mean_retorns_daily)**252)
cov_mat =StockReturns.cov()
cov_mat_annual = cov_mat*252
Volatility= np.sqrt(np.dot(arr.T, np.dot(cov_mat_annual, arr)))
return Returns, Volatility
def func(row):
random_portafolios['Volatility'].append(Volatility)
Returns, Volatility = complex_computation(row.values)
return pd.Series({'NewColumn1': Retturns,
'NewColumn2': Volatility})
def run_apply(random_portafolios):
df_result = random_portafolios.apply(func, axis=1)
return df_result