1. <legend id='kNFD0'><style id='kNFD0'><dir id='kNFD0'><q id='kNFD0'></q></dir></style></legend>

    2. <tfoot id='kNFD0'></tfoot>

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

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

        当在应用中也计算前一个值时,Pandas 中是否可以使用 dataframe.apply 中的前一行值?

        时间:2023-10-19
          <i id='nC1xC'><tr id='nC1xC'><dt id='nC1xC'><q id='nC1xC'><span id='nC1xC'><b id='nC1xC'><form id='nC1xC'><ins id='nC1xC'></ins><ul id='nC1xC'></ul><sub id='nC1xC'></sub></form><legend id='nC1xC'></legend><bdo id='nC1xC'><pre id='nC1xC'><center id='nC1xC'></center></pre></bdo></b><th id='nC1xC'></th></span></q></dt></tr></i><div id='nC1xC'><tfoot id='nC1xC'></tfoot><dl id='nC1xC'><fieldset id='nC1xC'></fieldset></dl></div>
          <legend id='nC1xC'><style id='nC1xC'><dir id='nC1xC'><q id='nC1xC'></q></dir></style></legend>

          <tfoot id='nC1xC'></tfoot>
            <bdo id='nC1xC'></bdo><ul id='nC1xC'></ul>
                    <tbody id='nC1xC'></tbody>

                • <small id='nC1xC'></small><noframes id='nC1xC'>

                  本文介绍了当在应用中也计算前一个值时,Pandas 中是否可以使用 dataframe.apply 中的前一行值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下数据框:

                   Index_Date    A    B    C    D
                   ===============================
                   2015-01-31    10   10   Nan  10
                   2015-02-01     2    3   Nan  22 
                   2015-02-02    10   60   Nan  280
                   2015-02-03    10   100   Nan  250
                  

                  要求:

                   Index_Date    A    B    C    D
                   ===============================
                   2015-01-31    10   10   10   10
                   2015-02-01     2    3   23   22
                   2015-02-02    10   60   290  280
                   2015-02-03    10   100  3000 250
                  

                  Column C 是通过取 Dvalue2015-01-31 派生的.

                  Column C is derived for 2015-01-31 by taking value of D.

                  那我需要将Cvalue用于2015-01-31并乘以value A2015-02-01 并添加 B.

                  Then I need to use the value of C for 2015-01-31 and multiply by the value of A on 2015-02-01 and add B.

                  我尝试了一个 apply 和一个 shift 使用一个 if else 这给出了一个关键错误.

                  I have attempted an apply and a shift using an if else by this gives a key error.

                  推荐答案

                  首先,创建派生值:

                  df.loc[0, 'C'] = df.loc[0, 'D']
                  

                  然后遍历剩余的行并填充计算值:

                  Then iterate through the remaining rows and fill the calculated values:

                  for i in range(1, len(df)):
                      df.loc[i, 'C'] = df.loc[i-1, 'C'] * df.loc[i, 'A'] + df.loc[i, 'B']
                  
                  
                    Index_Date   A   B    C    D
                  0 2015-01-31  10  10   10   10
                  1 2015-02-01   2   3   23   22
                  2 2015-02-02  10  60  290  280
                  

                  这篇关于当在应用中也计算前一个值时,Pandas 中是否可以使用 dataframe.apply 中的前一行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Python 迭代字符串中的每个字符 下一篇:Python 中“虽然不是 EOF"的完美对应物是什么?

                  相关文章

                  • <bdo id='9Mheq'></bdo><ul id='9Mheq'></ul>

                  <tfoot id='9Mheq'></tfoot>

                  <legend id='9Mheq'><style id='9Mheq'><dir id='9Mheq'><q id='9Mheq'></q></dir></style></legend>
                  <i id='9Mheq'><tr id='9Mheq'><dt id='9Mheq'><q id='9Mheq'><span id='9Mheq'><b id='9Mheq'><form id='9Mheq'><ins id='9Mheq'></ins><ul id='9Mheq'></ul><sub id='9Mheq'></sub></form><legend id='9Mheq'></legend><bdo id='9Mheq'><pre id='9Mheq'><center id='9Mheq'></center></pre></bdo></b><th id='9Mheq'></th></span></q></dt></tr></i><div id='9Mheq'><tfoot id='9Mheq'></tfoot><dl id='9Mheq'><fieldset id='9Mheq'></fieldset></dl></div>
                  1. <small id='9Mheq'></small><noframes id='9Mheq'>