久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" id='Onn65'><tfoot id='Onn65'></tfoot><dl id='Onn65'><fieldset id='Onn65'></fieldset></dl></div>

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

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

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

        在所有 pandas 列中將字符串轉換為浮點數,這是可

        Convert strings to float in all pandas columns, where this is possible(在所有 pandas 列中將字符串轉換為浮點數,這是可能的)

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

              <tfoot id='0n79K'></tfoot>
                <tbody id='0n79K'></tbody>
              <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 class="qwawimqqmiuu" id='0n79K'><tfoot id='0n79K'></tfoot><dl id='0n79K'><fieldset id='0n79K'></fieldset></dl></div>

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

                  本文介紹了在所有 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  <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>
                        <tbody id='6YpJU'></tbody>

                    • <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 class="qwawimqqmiuu" id='6YpJU'><tfoot id='6YpJU'></tfoot><dl id='6YpJU'><fieldset id='6YpJU'></fieldset></dl></div>
                        • <small id='6YpJU'></small><noframes id='6YpJU'>

                          1. 主站蜘蛛池模板: 久久99精品久久久 | 久久精品国产99国产精品 | 一级黄a | 国产免费观看一区 | 久久久国产一区二区三区四区小说 | 免费99精品国产自在在线 | 日韩在线中文 | 日韩在线精品强乱中文字幕 | 在线播放亚洲 | 亚洲精品中文字幕在线观看 | 欧美jizzhd精品欧美巨大免费 | 久久久久国产一区二区三区四区 | 国产精品欧美日韩 | 农村黄性色生活片 | 成人精品| 精品免费视频 | 成人免费观看视频 | 久久日韩粉嫩一区二区三区 | 秋霞a级毛片在线看 | 欧美亚洲一区二区三区 | 麻豆精品国产免费 | 亚洲欧美国产精品一区二区 | 日韩一二区在线观看 | 日韩欧美视频网站 | 在线观看你懂的网站 | h在线播放 | 欧美日韩电影一区二区 | 精品国产乱码久久久久久丨区2区 | 亚洲黄色av | 国产欧美精品一区 | 成人h视频 | 日韩美av | 精品欧美一区二区精品久久 | 中文字幕视频一区 | 久久综合伊人 | 国产一区二区影院 | 免费观看色 | 国产成人久久精品一区二区三区 | 欧美特级黄色 | 亚洲视频在线看 | 武道仙尊动漫在线观看 |