1. <i id='dECVt'><tr id='dECVt'><dt id='dECVt'><q id='dECVt'><span id='dECVt'><b id='dECVt'><form id='dECVt'><ins id='dECVt'></ins><ul id='dECVt'></ul><sub id='dECVt'></sub></form><legend id='dECVt'></legend><bdo id='dECVt'><pre id='dECVt'><center id='dECVt'></center></pre></bdo></b><th id='dECVt'></th></span></q></dt></tr></i><div id='dECVt'><tfoot id='dECVt'></tfoot><dl id='dECVt'><fieldset id='dECVt'></fieldset></dl></div>

  2. <legend id='dECVt'><style id='dECVt'><dir id='dECVt'><q id='dECVt'></q></dir></style></legend>

  3. <small id='dECVt'></small><noframes id='dECVt'>

    <tfoot id='dECVt'></tfoot>
    • <bdo id='dECVt'></bdo><ul id='dECVt'></ul>

      Python函数只返回第一个值而不是数据框

      时间:2023-09-02
    1. <small id='NHWlo'></small><noframes id='NHWlo'>

        <i id='NHWlo'><tr id='NHWlo'><dt id='NHWlo'><q id='NHWlo'><span id='NHWlo'><b id='NHWlo'><form id='NHWlo'><ins id='NHWlo'></ins><ul id='NHWlo'></ul><sub id='NHWlo'></sub></form><legend id='NHWlo'></legend><bdo id='NHWlo'><pre id='NHWlo'><center id='NHWlo'></center></pre></bdo></b><th id='NHWlo'></th></span></q></dt></tr></i><div id='NHWlo'><tfoot id='NHWlo'></tfoot><dl id='NHWlo'><fieldset id='NHWlo'></fieldset></dl></div>

          <bdo id='NHWlo'></bdo><ul id='NHWlo'></ul>

          <tfoot id='NHWlo'></tfoot>
            <tbody id='NHWlo'></tbody>
              <legend id='NHWlo'><style id='NHWlo'><dir id='NHWlo'><q id='NHWlo'></q></dir></style></legend>

              1. 本文介绍了Python函数只返回第一个值而不是数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我已经构建了一个函数,我将 5 个投资组合的回报附加到我想要返回到变量的数据框中.当我逐行运行函数中的命令(一种调试)时,我最终得到具有正确数量的值(例如 5)的变量 'folioReturn'(这是我希望我的脚本返回的那个).但是如果我调用该函数,则只返回数据帧的第一个值.有谁知道我怎样才能得到整个数据框?

                I have build a function where I append the returns of 5 portfolios to a dataframe which I want to return to a variable . When I run the commands within the function row by row(kind of debugging) I end upwith the variable 'folioReturn'(which is the one I want my script to return) having the right amount of values (e.x 5). But if I call the function, only the first value of the dataframe is returned. Does anyone know how I can get the whole dataframe ?

                
                def portfolioReturns (securities, quintilesNo, perReturns):
                    '''
                    this function receives 
                    1)securities: array with the security names and values ** for the purpose of our work the names
                    should already be sorted
                    2)quintilesNo: the number of portfolios we want to create 
                    3)perReturns: an array with the returns that will be used for performance measuremnt
                
                    It returns an array with the returns for each portfolio
                
                    '''
                
                    # we calculate the number of securities per portfolio 
                    stdFolioSize = np.divmod(securities.size, quintilesNo)[0] # we take the floor division
                    folioReturn = [] # pd.DataFrame()
                    # we create portfolios with equal number of securities except of the last one where we use all the remaining securities
                    for k in range(0, quintilesNo, 1): # in folio list we store the name of the securities we must include in each portfolio
                        if k < (quintilesNo - 1):           
                            folioList = securities.index.get_level_values(1)[k * stdFolioSize : (k + 1) * stdFolioSize]
                
                        else: # the last portfolio will also include the remainder securities 
                            folioList = securities.index.get_level_values(1)[k * stdFolioSize : securities.size]
                
                        # now that we have the list of the securities to be included in the folio, we use the table
                        # with the periodical returns to check the performance. The portfolio we construct is equally weighted
                
                        # first we drop one index(the first index of the country) and then we store all the periodical returns in one-array 
                        perRetFinalTable = pd.DataFrame(perReturns.reset_index(level = 0, drop = True)).T  
                
                        # using the list of the bonds we want to include in our portfolio we pick the bond returns and
                        # we store them in one array. Then we calculate the folio return
                        folio = perRetFinalTable[folioList]
                        folioReturn = np.append(folioReturn, folio.sum(axis = 1) * (1 / folio.size))
                        folioReturn = pd.DataFrame(folioReturn).T
                        # folioReturn = pd.Series(folioReturn).T
                
                        return (folioReturn)
                
                

                推荐答案

                return 语句必须在 for 循环之后,如果你想要整个列表在你的情况下在第一个循环中只返回值.只需从 for 循环中删除 return 它就可以正常工作.

                return statement must be after the for loop if you want whole list in your case during the first loop only the value is returned. just remove the return from for loop it will work fine.

                def portfolioReturns (securities, quintilesNo, perReturns):
                    '''
                    this function receives 
                    1)securities: array with the security names and values ** for the purpose of our work the names
                    should already be sorted
                    2)quintilesNo: the number of portfolios we want to create 
                    3)perReturns: an array with the returns that will be used for performance measuremnt
                
                    It returns an array with the returns for each portfolio
                
                    '''
                
                    # we calculate the number of securities per portfolio 
                    stdFolioSize = np.divmod(securities.size, quintilesNo)[0] # we take the floor division
                    folioReturn = [] # pd.DataFrame()
                    # we create portfolios with equal number of securities except of the last one where we use all the remaining securities
                    for k in range(0, quintilesNo, 1): # in folio list we store the name of the securities we must include in each portfolio
                        if k < (quintilesNo - 1):           
                            folioList = securities.index.get_level_values(1)[k * stdFolioSize : (k + 1) * stdFolioSize]
                
                        else: # the last portfolio will also include the remainder securities 
                            folioList = securities.index.get_level_values(1)[k * stdFolioSize : securities.size]
                
                        # now that we have the list of the securities to be included in the folio, we use the table
                        # with the periodical returns to check the performance. The portfolio we construct is equally weighted
                
                        # first we drop one index(the first index of the country) and then we store all the periodical returns in one-array 
                        perRetFinalTable = pd.DataFrame(perReturns.reset_index(level = 0, drop = True)).T  
                
                        # using the list of the bonds we want to include in our portfolio we pick the bond returns and
                        # we store them in one array. Then we calculate the folio return
                        folio = perRetFinalTable[folioList]
                        folioReturn = np.append(folioReturn, folio.sum(axis = 1) * (1 / folio.size))
                        folioReturn = pd.DataFrame(folioReturn).T
                        # folioReturn = pd.Series(folioReturn).T
                
                    return (folioReturn)
                

                这篇关于Python函数只返回第一个值而不是数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何从函数python返回一个int值 下一篇:有返回问题的总和的所有路径

                相关文章

                <small id='CVO5j'></small><noframes id='CVO5j'>

                • <bdo id='CVO5j'></bdo><ul id='CVO5j'></ul>
                1. <i id='CVO5j'><tr id='CVO5j'><dt id='CVO5j'><q id='CVO5j'><span id='CVO5j'><b id='CVO5j'><form id='CVO5j'><ins id='CVO5j'></ins><ul id='CVO5j'></ul><sub id='CVO5j'></sub></form><legend id='CVO5j'></legend><bdo id='CVO5j'><pre id='CVO5j'><center id='CVO5j'></center></pre></bdo></b><th id='CVO5j'></th></span></q></dt></tr></i><div id='CVO5j'><tfoot id='CVO5j'></tfoot><dl id='CVO5j'><fieldset id='CVO5j'></fieldset></dl></div>
                  <tfoot id='CVO5j'></tfoot>

                  <legend id='CVO5j'><style id='CVO5j'><dir id='CVO5j'><q id='CVO5j'></q></dir></style></legend>