【问题标题】:df Objects to floatdf 要浮动的对象
【发布时间】:2022-01-19 13:40:28
【问题描述】:

我有一个问题,我有这个 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

我需要你的帮助

【问题讨论】:

    标签: python pandas dataframe type-conversion


    【解决方案1】:

    “NOX”列包含不能转换为浮点数的字符串(类似于“MMC”的字符串)。但是,其他列可以转换为浮点数,然后在您的神经网络中使用。

    【讨论】:

      【解决方案2】:

      你可以试试:

      gd['NOX'].apply(pd.to_numeric, args=('coerce',))
      

      【讨论】:

        【解决方案3】:

        数据框中的“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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-22
          • 2020-09-06
          • 1970-01-01
          • 2023-03-09
          • 2014-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多