df 要浮动的对象
df Objects to float
我有一个问题我有这个 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
请问我需要你的帮助
你可以试试:
gd['NOX'].apply(pd.to_numeric, args=('coerce',))
“NOX”列包含不可转换为浮点数的字符串(类似于“MMC”的字符串)。但是,其他列可以转换为浮点数,然后在您的神经网络中使用。
数据框中的 '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)
我有一个问题我有这个 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
请问我需要你的帮助
你可以试试:
gd['NOX'].apply(pd.to_numeric, args=('coerce',))
“NOX”列包含不可转换为浮点数的字符串(类似于“MMC”的字符串)。但是,其他列可以转换为浮点数,然后在您的神经网络中使用。
数据框中的 '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)