<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

        Remap values in pandas column with a dict, preserve NaNs(使用 dict 重新映射 pandas 列中的值,保留 NaN)
          • <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                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>
                    • <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>

                          <tbody id='3n4Gy'></tbody>