<bdo id='0n79K'></bdo><ul id='0n79K'></ul>

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

        <small id='0n79K'></small><noframes id='0n79K'>

        在所有 pandas 列中将字符串转换为浮点数,这是可

        时间:2023-08-05
        <legend id='Onn65'><style id='Onn65'><dir id='Onn65'><q id='Onn65'></q></dir></style></legend>

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

              <tfoot id='Onn65'></tfoot>

                <tbody id='Onn65'></tbody>

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

                • <bdo id='Onn65'></bdo><ul id='Onn65'></ul>
                  本文介绍了在所有 pandas 列中将字符串转换为浮点数,这是可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我从列表列表中创建了一个 pandas 数据框

                  I created a pandas dataframe from a list of lists

                  import pandas as pd
                  
                  df_list = [["a", "1", "2"], ["b", "3", np.nan]]
                  df = pd.DataFrame(df_list, columns = list("ABC"))
                  >>>   A  B    C
                     0  a  1    2
                     1  b  3  NaN
                  

                  有没有办法将数据框的所有列转换为可以转换的浮点数,即 B 和 C?如果您知道要转换哪些列,则可以使用以下方法:

                  Is there a way to convert all columns of the dataframe to float, that can be converted, i.e. B and C? The following works, if you know, which columns to convert:

                    df[["B", "C"]] = df[["B", "C"]].astype("float")
                  

                  但是,如果您事先不知道哪些列包含数字,您会怎么做?当我尝试时

                  But what do you do, if you don't know in advance, which columns contain the numbers? When I tried

                    df = df.astype("float", errors = "ignore")
                  

                  所有列仍然是字符串/对象.同样,

                  all columns are still strings/objects. Similarly,

                  df[["B", "C"]] = df[["B", "C"]].apply(pd.to_numeric)
                  

                  转换两列(虽然B"是 int 而C"是float",因为存在 NaN 值),但是

                  converts both columns (though "B" is int and "C" is "float", because of the NaN value being present), but

                  df = df.apply(pd.to_numeric)
                  

                  显然会引发错误消息,我看不出有什么方法可以抑制它.
                  是否有可能在不遍历每一列的情况下执行此字符串-浮点转换,以尝试 .astype("float", errors = "ignore")?

                  obviously throws an error message and I don't see a way to suppress this.
                  Is there a possibility to perform this string-float conversion without looping through each column, to try .astype("float", errors = "ignore")?

                  推荐答案

                  我觉得你需要errors='ignore'pandas-docs/stable/generated/pandas.to_numeric.html" rel="noreferrer">to_numeric:

                  I think you need parameter errors='ignore' in to_numeric:

                  df = df.apply(pd.to_numeric, errors='ignore')
                  print (df.dtypes)
                  A     object
                  B      int64
                  C    float64
                  dtype: object
                  

                  如果不是混合值,它工作得很好 - 带有字符串的数字:

                  It working nice if not mixed values - numeric with strings:

                  df_list = [["a", "t", "2"], ["b", "3", np.nan]]
                  df = pd.DataFrame(df_list, columns = list("ABC"))
                  
                  df = df.apply(pd.to_numeric, errors='ignore')
                  print (df)
                     A  B    C
                  0  a  t  2.0 <=added t to column B for mixed values
                  1  b  3  NaN
                  
                  print (df.dtypes)
                  A     object
                  B     object
                  C    float64
                  dtype: object
                  

                  您也可以将 int 向下转换为 floats:

                  You can downcast also int to floats:

                  df = df.apply(pd.to_numeric, errors='ignore', downcast='float')
                  print (df.dtypes)
                  A     object
                  B    float32
                  C    float32
                  dtype: object
                  

                  同理:

                  df = df.apply(lambda x: pd.to_numeric(x, errors='ignore', downcast='float'))
                  print (df.dtypes)
                  A     object
                  B    float32
                  C    float32
                  dtype: object
                  

                  这篇关于在所有 pandas 列中将字符串转换为浮点数,这是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 python 中将字节对象转换为十进制或二进制 下一篇:为什么调用 Python 的“魔术方法"不像对应的运

                  相关文章

                  最新文章

                  <legend id='6YpJU'><style id='6YpJU'><dir id='6YpJU'><q id='6YpJU'></q></dir></style></legend>

                      <bdo id='6YpJU'></bdo><ul id='6YpJU'></ul>
                    <tfoot id='6YpJU'></tfoot>

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