久久久久久久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 列中將字符串轉(zhuǎn)換為浮點(diǎn)數(shù),這是可

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

            <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 列中將字符串轉(zhuǎn)換為浮點(diǎn)數(shù),這是可能的的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我從列表列表中創(chuàng)建了一個(gè) pandas 數(shù)據(jù)框

                  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
                  

                  有沒(méi)有辦法將數(shù)據(jù)框的所有列轉(zhuǎn)換為可以轉(zhuǎn)換的浮點(diǎn)數(shù),即 B 和 C?如果您知道要轉(zhuǎn)換哪些列,則可以使用以下方法:

                  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")
                  

                  但是,如果您事先不知道哪些列包含數(shù)字,您會(huì)怎么做?當(dāng)我嘗試時(shí)

                  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")
                  

                  所有列仍然是字符串/對(duì)象.同樣,

                  all columns are still strings/objects. Similarly,

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

                  轉(zhuǎn)換兩列(雖然B"是 int 而C"是float",因?yàn)榇嬖?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)
                  

                  顯然會(huì)引發(fā)錯(cuò)誤消息,我看不出有什么方法可以抑制它.
                  是否有可能在不遍歷每一列的情況下執(zhí)行此字符串-浮點(diǎn)轉(zhuǎn)換,以嘗試 .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")?

                  推薦答案

                  我覺(jué)得你需要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
                  

                  如果不是混合值,它工作得很好 - 帶有字符串的數(shù)字:

                  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 向下轉(zhuǎn)換為 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
                  

                  這篇關(guān)于在所有 pandas 列中將字符串轉(zhuǎn)換為浮點(diǎn)數(shù),這是可能的的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動(dòng)后進(jìn)度躍升至 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 刻度標(biāo)簽設(shè)置在固定位置,以便當(dāng)我向左或向右滾動(dòng)時(shí),yaxis 刻度標(biāo)簽應(yīng)該可見(jiàn)
                  `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時(shí)顯示進(jìn)度條?)
                  <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. 主站蜘蛛池模板: 亚洲一区二区av | 精品av| 国产中文视频 | 久久免费精品视频 | 欧美一级三级在线观看 | 麻豆久久久久久 | 国产精品一区二 | 精品一区二区三区四区在线 | 欧美一区二区黄 | 亚洲+变态+欧美+另类+精品 | 欧美精品99 | 久久亚洲91 | 婷婷综合激情 | 亚洲视频在线看 | 久久综合影院 | 在线中文字幕日韩 | 久在线| 日韩久久综合网 | 国产精品久久久久久吹潮 | 欧美成人一级视频 | 成人在线免费观看av | 亚洲av毛片| 婷婷在线网站 | 日日操视频 | 欧美xxxx黑人又粗又长 | 色久电影 | 欧美精品第一区 | av一区二区三区四区 | 久久中文字幕一区 | 国产人久久人人人人爽 | 国精日本亚洲欧州国产中文久久 | 一区二区国产精品 | 欧美精品一区二区三区在线播放 | 日韩视频区 | 色悠悠久 | 欧美成人免费在线 | 国产 91 视频| 97超碰人人草 | 亚洲精品一 | 日韩午夜电影 | 天天插天天干 |