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

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

        • <bdo id='GSJ9Y'></bdo><ul id='GSJ9Y'></ul>

      1. 如何将数据框中的真假值转换为 1 为真,0 为假

        How to convert true false values in dataframe as 1 for true and 0 for false(如何将数据框中的真假值转换为 1 为真,0 为假)

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

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

                  <tbody id='sQCzl'></tbody>
                <legend id='sQCzl'><style id='sQCzl'><dir id='sQCzl'><q id='sQCzl'></q></dir></style></legend>
                • <bdo id='sQCzl'></bdo><ul id='sQCzl'></ul>
                • 本文介绍了如何将数据框中的真假值转换为 1 为真,0 为假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何将Dataframe中的真假值转换为1为真,0为假

                  How to convert true false values in Dataframe as 1 for true and 0 for false

                  COL1  COL2 COL3  COL4
                  12   TRUE  14    FALSE
                  13   FALSE  13    TRUE
                  
                  
                  OUTPUT
                  12   1  14 0
                  13   0  13 1
                  

                  推荐答案

                  首先,如果你有字符串 'TRUE''FALSE',你可以将它们转换像这样布尔 TrueFalse 值:

                  First, if you have the strings 'TRUE' and 'FALSE', you can convert those to boolean True and False values like this:

                  df['COL2'] == 'TRUE'
                  

                  这会给你一个 bool 列.可以使用 astype 转换为 int (因为 bool 是整数类型,其中 True 表示 1False 表示 0,这正是你想要的):

                  That gives you a bool column. You can use astype to convert to int (because bool is an integral type, where True means 1 and False means 0, which is exactly what you want):

                  (df['COL2'] == 'TRUE').astype(int)
                  

                  要用这个新的 int 列替换旧的字符串列,只需分配它:

                  To replace the old string column with this new int column, just assign it:

                  df['COL2'] = (df['COL2'] == 'TRUE').astype(int)
                  

                  要同时对两列执行此操作,只需使用列列表进行索引:

                  And to do that to two columns at one, just index with a list of columns:

                  df[['COL2', 'COL4']] = (df[['COL2', 'COL4']] == 'TRUE').astype(int)
                  

                  这篇关于如何将数据框中的真假值转换为 1 为真,0 为假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <bdo id='NPy5x'></bdo><ul id='NPy5x'></ul>
                    <i id='NPy5x'><tr id='NPy5x'><dt id='NPy5x'><q id='NPy5x'><span id='NPy5x'><b id='NPy5x'><form id='NPy5x'><ins id='NPy5x'></ins><ul id='NPy5x'></ul><sub id='NPy5x'></sub></form><legend id='NPy5x'></legend><bdo id='NPy5x'><pre id='NPy5x'><center id='NPy5x'></center></pre></bdo></b><th id='NPy5x'></th></span></q></dt></tr></i><div id='NPy5x'><tfoot id='NPy5x'></tfoot><dl id='NPy5x'><fieldset id='NPy5x'></fieldset></dl></div>

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

                          <tbody id='NPy5x'></tbody>
                      1. <tfoot id='NPy5x'></tfoot>