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

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

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

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

        TypeError:按多列分组时,无法将bool转换为numpy.ndarray&quot;

        时间:2024-08-21
      2. <i id='U4eNC'><tr id='U4eNC'><dt id='U4eNC'><q id='U4eNC'><span id='U4eNC'><b id='U4eNC'><form id='U4eNC'><ins id='U4eNC'></ins><ul id='U4eNC'></ul><sub id='U4eNC'></sub></form><legend id='U4eNC'></legend><bdo id='U4eNC'><pre id='U4eNC'><center id='U4eNC'></center></pre></bdo></b><th id='U4eNC'></th></span></q></dt></tr></i><div id='U4eNC'><tfoot id='U4eNC'></tfoot><dl id='U4eNC'><fieldset id='U4eNC'></fieldset></dl></div>

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

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

              <legend id='U4eNC'><style id='U4eNC'><dir id='U4eNC'><q id='U4eNC'></q></dir></style></legend>
              1. <tfoot id='U4eNC'></tfoot>
                    <tbody id='U4eNC'></tbody>
                  本文介绍了TypeError:按多列分组时,无法将bool转换为numpy.ndarray&quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想按两列对数据帧进行分组,以汇总每家商店的月平均销售额。

                  数据(fact pandas 数据帧):

                  store_id    sku_id  date    quantity    city    city    category    month
                  0   354 31253   2017-08-08  1   Paris   Paris   Shirt   8
                  1   354 31253   2017-08-19  1   Paris   Paris   Shirt   8
                  2   354 31258   2017-07-30  1   Paris   Paris   Shirt   7
                  3   354 277171  2017-09-28  1   Paris   Paris   Shirt   9
                  4   174 295953  2017-08-16  1   London  London  Shirt   8
                  

                  基于store_idmonth的分组只能正常工作,但是当我尝试同时按store_idmonth分组时,我得到:

                  groupby_month = fact['quantity'].groupby(fact['store_id', 'month'])
                  
                  ---------------------------------------------------------------------------
                  TypeError                                 Traceback (most recent call last)
                  <ipython-input-169-a8cffb72ab7c> in <module>
                  ----> 1 groupby_month = fact['quantity'].groupby(fact['store_id', 'month'])
                        2 
                        3 
                  
                  D:Anaconda3libsite-packagespandascoreframe.py in __getitem__(self, key)
                     2925             if self.columns.nlevels > 1:
                     2926                 return self._getitem_multilevel(key)
                  -> 2927             indexer = self.columns.get_loc(key)
                     2928             if is_integer(indexer):
                     2929                 indexer = [indexer]
                  
                  D:Anaconda3libsite-packagespandascoreindexesase.py in get_loc(self, key, method, tolerance)
                     2655                                  'backfill or nearest lookups')
                     2656             try:
                  -> 2657                 return self._engine.get_loc(key)
                     2658             except KeyError:
                     2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
                  
                  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
                  
                  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
                  
                  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine._get_loc_duplicates()
                  
                  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine._maybe_get_bool_indexer()
                  
                  TypeError: Cannot convert bool to numpy.ndarray
                  

                  推荐答案

                  首先检查索引标签和列

                  fact.index 
                  fact.columns
                  

                  如果需要将索引转换为列,请使用:

                  使用:

                  fact.reset_index()
                  

                  然后您可以使用:

                  fact.groupby(['store_id', 'month'])['quantity'].mean()
                  

                  输出:

                  store_id  month
                  174       8        1
                  354       7        1
                            8        1
                            9        1
                  Name: quantity, dtype: int64
                  

                  或更好:

                  fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
                  print(fact)
                     store_id  sku_id        date  quantity    city  city.1 category  month  
                  0       354   31253  2017-08-08         1   Paris   Paris    Shirt      8   
                  1       354   31253  2017-08-19         1   Paris   Paris    Shirt      8   
                  2       354   31258  2017-07-30         1   Paris   Paris    Shirt      7   
                  3       354  277171  2017-09-28         1   Paris   Paris    Shirt      9   
                  4       174  295953  2017-08-16         1  London  London    Shirt      8   
                  
                     mean  
                  0     1  
                  1     1  
                  2     1  
                  3     1  
                  4     1  
                  

                  这篇关于TypeError:按多列分组时,无法将bool转换为numpy.ndarray&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:基于数据帧中数字连续出现的条件概率计算 下一篇:优雅地编写np.WHERE表示列中的不同值

                  相关文章

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

                    <tfoot id='JllZi'></tfoot>

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

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