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

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

    1. <tfoot id='DVTL7'></tfoot>

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

        Python:检查值属于哪个bin

        Python: Checking to which bin a value belongs(Python:检查值属于哪个bin)
        • <tfoot id='LK4lj'></tfoot>

                <tbody id='LK4lj'></tbody>

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

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

                • <bdo id='LK4lj'></bdo><ul id='LK4lj'></ul>
                • <i id='LK4lj'><tr id='LK4lj'><dt id='LK4lj'><q id='LK4lj'><span id='LK4lj'><b id='LK4lj'><form id='LK4lj'><ins id='LK4lj'></ins><ul id='LK4lj'></ul><sub id='LK4lj'></sub></form><legend id='LK4lj'></legend><bdo id='LK4lj'><pre id='LK4lj'><center id='LK4lj'></center></pre></bdo></b><th id='LK4lj'></th></span></q></dt></tr></i><div id='LK4lj'><tfoot id='LK4lj'></tfoot><dl id='LK4lj'><fieldset id='LK4lj'></fieldset></dl></div>
                  本文介绍了Python:检查值属于哪个bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a list of values and a list of bin edges. Now I need to check for all values to what bin they belong to. Is there a more pythonic way than iterating over the values and then over the bins and checking if the value belongs to the current bin, like:

                  my_list = [3,2,56,4,32,4,7,88,4,3,4]
                  bins = [0,20,40,60,80,100]
                  
                  for i in my_list:
                      for j in range(len(bins)):
                          if bins(j) < i < bins(j+1):
                              DO SOMETHING
                  

                  This doesn't look very pretty to me. Thanks!

                  解决方案

                  Probably too late, but for future reference, numpy has a function that does just that:

                  http://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html

                  >>> my_list = [3,2,56,4,32,4,7,88,4,3,4]
                  >>> bins = [0,20,40,60,80,100]
                  >>> np.digitize(my_list,bins)
                  array([1, 1, 3, 1, 2, 1, 1, 5, 1, 1, 1])
                  

                  The result is an array of indexes corresponding to the bin from bins that each element from my_list belongs too. Note that the function will also bin values that fall outside of your first and last bin edges:

                  >>> my_list = [-5,200]
                  >>> np.digitize(my_list,bins)
                  array([0, 6])
                  

                  And Pandas has something like it too:

                  http://pandas.pydata.org/pandas-docs/dev/basics.html#discretization-and-quantiling

                  >>> pd.cut(my_list, bins)
                  Categorical: 
                  array(['(0, 20]', '(0, 20]', '(40, 60]', '(0, 20]', '(20, 40]', '(0, 20]',
                         '(0, 20]', '(80, 100]', '(0, 20]', '(0, 20]', '(0, 20]'], dtype=object)
                  Levels (5): Index(['(0, 20]', '(20, 40]', '(40, 60]', '(60, 80]',
                                     '(80, 100]'], dtype=object)
                  

                  这篇关于Python:检查值属于哪个bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

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

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