<tfoot id='m3DAB'></tfoot>
  • <legend id='m3DAB'><style id='m3DAB'><dir id='m3DAB'><q id='m3DAB'></q></dir></style></legend>

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

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

          <bdo id='m3DAB'></bdo><ul id='m3DAB'></ul>
      2. 使用 dict 重新映射 pandas 列中的值,保留 NaN

        时间:2024-04-21
          • <i id='WkjTY'><tr id='WkjTY'><dt id='WkjTY'><q id='WkjTY'><span id='WkjTY'><b id='WkjTY'><form id='WkjTY'><ins id='WkjTY'></ins><ul id='WkjTY'></ul><sub id='WkjTY'></sub></form><legend id='WkjTY'></legend><bdo id='WkjTY'><pre id='WkjTY'><center id='WkjTY'></center></pre></bdo></b><th id='WkjTY'></th></span></q></dt></tr></i><div id='WkjTY'><tfoot id='WkjTY'></tfoot><dl id='WkjTY'><fieldset id='WkjTY'></fieldset></dl></div>
              <tbody id='WkjTY'></tbody>

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

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

                  <tfoot id='WkjTY'></tfoot>
                1. 本文介绍了使用 dict 重新映射 pandas 列中的值,保留 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a dictionary which looks like this: di = {1: "A", 2: "B"}

                  I would like to apply it to the col1 column of a dataframe similar to:

                       col1   col2
                  0       w      a
                  1       1      2
                  2       2    NaN
                  

                  to get:

                       col1   col2
                  0       w      a
                  1       A      2
                  2       B    NaN
                  

                  How can I best do this? For some reason googling terms relating to this only shows me links about how to make columns from dicts and vice-versa :-/

                  解决方案

                  You can use .replace. For example:

                  >>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
                  >>> di = {1: "A", 2: "B"}
                  >>> df
                    col1 col2
                  0    w    a
                  1    1    2
                  2    2  NaN
                  >>> df.replace({"col1": di})
                    col1 col2
                  0    w    a
                  1    A    2
                  2    B  NaN
                  

                  or directly on the Series, i.e. df["col1"].replace(di, inplace=True).

                  这篇关于使用 dict 重新映射 pandas 列中的值,保留 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 3 中的 `raw_input()` 和 `input()` 有什么区别? 下一篇:ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()

                  相关文章

                2. <tfoot id='3n4Gy'></tfoot>

                    • <bdo id='3n4Gy'></bdo><ul id='3n4Gy'></ul>

                    <legend id='3n4Gy'><style id='3n4Gy'><dir id='3n4Gy'><q id='3n4Gy'></q></dir></style></legend>
                  1. <small id='3n4Gy'></small><noframes id='3n4Gy'>

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