<tfoot id='V6wO5'></tfoot>

        <bdo id='V6wO5'></bdo><ul id='V6wO5'></ul>
    1. <legend id='V6wO5'><style id='V6wO5'><dir id='V6wO5'><q id='V6wO5'></q></dir></style></legend>

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

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

        pandas 数据框条件 .mean() 取决于特定列中的值

        时间:2023-08-29
      2. <tfoot id='FiFl2'></tfoot>
            <tbody id='FiFl2'></tbody>

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

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

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

                  <legend id='FiFl2'><style id='FiFl2'><dir id='FiFl2'><q id='FiFl2'></q></dir></style></legend>
                  本文介绍了 pandas 数据框条件 .mean() 取决于特定列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个新列,该列返回同一 df 中现有列的值的平均值.但是,平均值应根据其他三列中的分组来计算.

                  I'm trying to create a new column which returns the mean of values from an existing column in the same df. However the mean should be computed based on a grouping in three other columns.

                  Out[184]: 
                     YEAR daytype hourtype  scenario  option_value    
                  0  2015     SAT     of_h         0      0.134499       
                  1  2015     SUN     of_h         1     63.019250      
                  2  2015     WD      of_h         2     52.113516       
                  3  2015     WD      pk_h         3     43.126513       
                  4  2015     SAT     of_h         4     56.431392 
                  

                  当YEAR"、daytype"和hourtype"相似时,我基本上希望有一个新列mean"来计算option value"的平均值.

                  I basically would like to have a new column 'mean' which compute the mean of "option value", when "YEAR", "daytype", and "hourtype" are similar.

                  我尝试了以下方法但没有成功...

                  I tried the following approach but without success ...

                  In [185]: o2['premium']=o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_cf'].mean()
                  
                  TypeError: incompatible index of inserted column with frame index
                  

                  推荐答案

                  这是一种方法

                  In [19]: def cust_mean(grp):
                     ....:     grp['mean'] = grp['option_value'].mean()
                     ....:     return grp
                     ....:
                  
                  In [20]: o2.groupby(['YEAR', 'daytype', 'hourtype']).apply(cust_mean)
                  Out[20]:
                     YEAR daytype hourtype  scenario  option_value       mean
                  0  2015     SAT     of_h         0      0.134499  28.282946
                  1  2015     SUN     of_h         1     63.019250  63.019250
                  2  2015      WD     of_h         2     52.113516  52.113516
                  3  2015      WD     pk_h         3     43.126513  43.126513
                  4  2015     SAT     of_h         4     56.431392  28.282946
                  

                  那么,你的尝试出了什么问题?

                  So, what was going wrong with your attempt?

                  它返回一个与原始数据框形状不同的聚合.

                  It returns an aggregate with different shape from the original dataframe.

                  In [21]: o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_value'].mean()
                  Out[21]:
                  YEAR  daytype  hourtype
                  2015  SAT      of_h        28.282946
                        SUN      of_h        63.019250
                        WD       of_h        52.113516
                                 pk_h        43.126513
                  Name: option_value, dtype: float64
                  

                  或者使用变换

                  In [1461]: o2['premium'] = (o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_value']
                                                .transform('mean'))
                  
                  In [1462]: o2
                  Out[1462]:
                     YEAR daytype hourtype  scenario  option_value    premium
                  0  2015     SAT     of_h         0      0.134499  28.282946
                  1  2015     SUN     of_h         1     63.019250  63.019250
                  2  2015      WD     of_h         2     52.113516  52.113516
                  3  2015      WD     pk_h         3     43.126513  43.126513
                  4  2015     SAT     of_h         4     56.431392  28.282946
                  

                  这篇关于 pandas 数据框条件 .mean() 取决于特定列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何迭代 pandas 数据框并创建新列 下一篇:Numpy:按多个条件过滤行?

                  相关文章

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

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

                      <tfoot id='p6HaI'></tfoot>
                        <bdo id='p6HaI'></bdo><ul id='p6HaI'></ul>

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