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

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

        获取支持 NA/的布尔 pandas 列可以为空

        时间:2023-09-01
        <i id='DBGr4'><tr id='DBGr4'><dt id='DBGr4'><q id='DBGr4'><span id='DBGr4'><b id='DBGr4'><form id='DBGr4'><ins id='DBGr4'></ins><ul id='DBGr4'></ul><sub id='DBGr4'></sub></form><legend id='DBGr4'></legend><bdo id='DBGr4'><pre id='DBGr4'><center id='DBGr4'></center></pre></bdo></b><th id='DBGr4'></th></span></q></dt></tr></i><div id='DBGr4'><tfoot id='DBGr4'></tfoot><dl id='DBGr4'><fieldset id='DBGr4'></fieldset></dl></div>
          <tbody id='DBGr4'></tbody>

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

              • <bdo id='DBGr4'></bdo><ul id='DBGr4'></ul>
                <tfoot id='DBGr4'></tfoot>
              • <legend id='DBGr4'><style id='DBGr4'><dir id='DBGr4'><q id='DBGr4'></q></dir></style></legend>

                  本文介绍了获取支持 NA/的布尔 pandas 列可以为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何创建一个支持 Nan/缺失值的 dtype bool(或 int)的 pandas 数据框列?

                  How can I create a pandas dataframe column with dtype bool (or int for that matter) with support for Nan/missing values?

                  当我这样尝试时:

                  d = {'one' : np.ma.MaskedArray([True, False, True, True], mask = [0,0,1,0]),
                  'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
                  df = pd.DataFrame(d)
                  print (df.dtypes)
                  print (df)
                  

                  column one 被隐式转换为对象.ints 也类似:

                  column one is implicitly converted to object. Likewise similar for ints:

                  d = {'one' : np.ma.MaskedArray([1,3,2,1], mask = [0,0,1,0]),
                  'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
                  df = pd.DataFrame(d)
                  print (df.dtypes)
                  print (df)
                  

                  one 在这里隐式转换为 float64,如果我留在 int 域并且不使用它的特质(在比较、舍入误差等时总是有容忍度)

                  one is here implicitly converted to float64, and I'd prefer if I stayed in int domain and not handle floating point arithmetic with its idiosyncrasies (always have tolerance when comparing, rounding errors, etc.)

                  推荐答案

                  pandas >= 1.0

                  从 pandas 1.0.0(2020 年 1 月)开始,有 直接对可为空的布尔值进行实验性支持:

                  In [183]: df.one.astype('boolean')
                  Out[183]:
                  a     True
                  b    False
                  c     <NA>
                  d     True
                  Name: one, dtype: object
                  

                  在这个版本中,pandas 在整数情况下也将使用 pd.NA 代替 np.nan:

                  In this version, pandas will also use pd.NA instead of np.nan in the integer case:

                  In [166]: df.astype('Int64')
                  Out[166]:
                      one  two
                  a     1    1
                  b     3    2
                  c  <NA>    3
                  d     1    4
                  

                  <小时>

                  熊猫 >= 0.24

                  在整数情况下,从 pandas 0.24(2019 年 1 月)开始,您可以使用 可空整数 来实现你想要的:

                  In [165]: df
                  Out[165]:
                     one  two
                  a  1.0  1.0
                  b  3.0  2.0
                  c  NaN  3.0
                  d  1.0  4.0
                  
                  In [166]: df.astype('Int64')
                  Out[166]:
                     one  two
                  a    1    1
                  b    3    2
                  c  NaN    3
                  d    1    4
                  

                  这通过将支持数组转换为 arrays.IntegerArray,并且布尔值没有等效的东西,但是在 这个 GitHub 问题 和 这个 PyData 演讲.您可以编写自己的 扩展类型 来覆盖这种情况也是如此,但如果您可以接受由整数 0 和 1 表示的布尔值,则一种方法可能如下:

                  This works by converting the backing array to an arrays.IntegerArray, and there is no equivalent thing for booleans, but some work in that direction is discussed in this GitHub issue and this PyData talk. You could write your own extension type to cover this case as well, but if you can live with your booleans being represented by the integers 0 and 1, one approach could be the following:

                  In [183]: df.one
                  Out[183]:
                  a     True
                  b    False
                  c      NaN
                  d     True
                  Name: one, dtype: object
                  
                  In [184]: (df.one * 1).astype('Int64')
                  Out[184]:
                  a      1
                  b      0
                  c    NaN
                  d      1
                  Name: one, dtype: Int64
                  

                  这篇关于获取支持 NA/的布尔 pandas 列可以为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 bool(xml.etree.ElementTree.Element) 评估为 False? 下一篇:pandas 数据框将 INT64 列转换为布尔值

                  相关文章

                1. <small id='ZK0fN'></small><noframes id='ZK0fN'>

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

                      <tfoot id='ZK0fN'></tfoot>