我有一個問題我有這個 df :
**<class 'pandas.core.frame.DataFrame'>
RangeIndex: 44640 entries, 0 to 44639
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 NOx_Min_[ppm] 44640 non-null object
1 NOx_Min_[mg/m3N] 44640 non-null object
2 NOx_corr_Min_[mg/m3N] 44640 non-null object
3 NOX 44640 non-null object
dtypes: object(4)
memory usage: 1.4 MB
NOx_Min_[ppm] NOx_Min_[mg/m3N] NOx_corr_Min_[mg/m3N] NOX
0 0 0 0 MMC
1 0 0 0 MMC
2 0 0 0 MMC
3 0 0 0 MMC
4 0 0 0 MMC**
我正在嘗試將物件轉換為數字gd['NOX']=pd.to_numeric(gd['NOX']),然后通過我的神經網路對其進行處理,但它會產生以下錯誤:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
pandas/_libs/lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "MMC"
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-11-78184c2df2b2> in <module>()
----> 1 gd['NOX']=pd.to_numeric(gd['NOX'])
/usr/local/lib/python3.7/dist-packages/pandas/core/tools/numeric.py in to_numeric(arg, errors, downcast)
151 try:
152 values = lib.maybe_convert_numeric(
--> 153 values, set(), coerce_numeric=coerce_numeric
154 )
155 except (ValueError, TypeError):
pandas/_libs/lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "MMC" at position 0
lz我需要你的幫助
uj5u.com熱心網友回復:
“NOX”列包含不能轉換為浮點數的字串(類似于“MMC”的字串)。但是,其他列可以轉換為浮點數,然后在您的神經網路中使用。
uj5u.com熱心網友回復:
你可以試試:
gd['NOX'].apply(pd.to_numeric, args=('coerce',))
uj5u.com熱心網友回復:
資料框中的“NOX”列包含一個無法轉換為數值的字串值。在這種情況下,如果您在無法決議該值的單元格中接受 Nan(null) 值,只需在函式中設定 errors = "coerce" ,如下所示。
gd['NOX']=pd.to_numeric(gd['NOX'], errors = "coerce")
如果您需要將這些值決議為某個值,請使用以下并根據需要實作邏輯。
def cast_to_float(x):
try:
return float(x)
except ValueError:
return sum([ ord(i) for i in x]) # add your logic here
gd['NOX'] = gd['NOX'].apply(cast_to_float)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/415593.html
標籤:
上一篇:從單詞串列中查找字符出現百分比
下一篇:在django中上傳圖片
