【问题标题】:How convert "object" to numeric values including Nan如何将“对象”转换为数值,包括 Nan
【发布时间】:2018-01-07 07:14:49
【问题描述】:

我有这个数据很多年了,需要绘制不同年份的误差图,1993年被选为

fm93 = fmm[(fmm.Year == 1993)]

那么fm93数据框就是

Year    moB  m1     std1    co1 min1    max1    m2S   std2S co2S    min2S   max2S
1993    1   296.42  18.91   31  262.4   336      --   --   --       --      --
1993    2   280.76  24.59   28  239.4   329.3    --  --      --   --    --
1993    3   271.41  19.16   31  236.4   304.8   285.80  20.09   20  251.6   319.7
1993    4   287.98  22.52   30  245.9   341 296.75  21.77   27  261.1   345.7
1993    5   287.05  30.79   30  229.2   335.7   300.06  27.64   24  249.5   351.8
1993    6   288.65  11.29   4   275.9   301.9   263.70  73.40   7   156.5   361
1993    7   280.11  36.01   12  237 363 302.67  26.39   22  262.9   377.1
1993    8   296.51  34.55   31  234.8   372.9   305.85  39.95   28  234.1   417.9
1993    9   321.31  34.54   25  263.8   396 309.01  42.52   29  205.9   403.2
1993    10  315.80  8.63    2   309.7   321.9   288.65  35.86   31  230.9   345.4
1993    11  288.26  24.07   30  231.4   322.8   297.99  23.81   28  238 336.5
1993    12  296.87  18.31   31  257.6   331.5   303.02  20.02   29  265.7   340.7

当我尝试用 err std1 绘制 moB,m1 时出现错误

ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]

那是因为值是“对象”..

array([[1993, 1, '296.42', '18.91', '31', '262.4', '336', '--', '--', '--',
    '--', '--'],
   [1993, 2, '280.76', '24.59', '28', '239.4', '329.3', '--', '--',
    '--', '--', '--'],
   [1993, 3, '271.41', '19.16', '31', '236.4', '304.8', '285.80',
    '20.09', '20', '251.6', '319.7'],
   [1993, 4, '287.98', '22.52', '30', '245.9', '341', '296.75',
    '21.77', '27', '261.1', '345.7'],
   [1993, 5, '287.05', '30.79', '30', '229.2', '335.7', '300.06',
    '27.64', '24', '249.5', '351.8'],
   [1993, 6, '288.65', '11.29', '4', '275.9', '301.9', '263.70',
    '73.40', '7', '156.5', '361'],
   [1993, 7, '280.11', '36.01', '12', '237', '363', '302.67', '26.39',
    '22', '262.9', '377.1'],
   [1993, 8, '296.51', '34.55', '31', '234.8', '372.9', '305.85',
    '39.95', '28', '234.1', '417.9'],
   [1993, 9, '321.31', '34.54', '25', '263.8', '396', '309.01',
    '42.52', '29', '205.9', '403.2'],
   [1993, 10, '315.80', '8.63', '2', '309.7', '321.9', '288.65',
    '35.86', '31', '230.9', '345.4'],
   [1993, 11, '288.26', '24.07', '30', '231.4', '322.8', '297.99',
    '23.81', '28', '238', '336.5'],
   [1993, 12, '296.87', '18.31', '31', '257.6', '331.5', '303.02',
    '20.02', '29', '265.7', '340.7']], dtype=object)

我尝试用

转换这些数据
fm93_1 = fm93.astype('float64', raise_on_error = False)

但问题仍然存在......如何转换 Nan 值('--')或忽略以绘制我的结果? 提前致谢

【问题讨论】:

    标签: python-3.x pandas nan


    【解决方案1】:

    首先,您应该尝试在“--”之后绘制数据样本,看看是否可以生成图。你可以试试:

    # This should plot from row 3 onwards, omitting row 1 and 2
    df.iloc[3:].plot()
    

    假设 '--' 是问题所在,您可以使用 np.NaN replace。未绘制 NaN 值。

    df.replace('--', np.NaN, inplace=True)
    df.plot()
    

    另一种方法是选择不包含'--'的df:

    mask = df[df == '--'].any(axis=1) # Check if row contains '--'
    valid_indexes = df[mask == False].index # Return index for rows w/o '--'
    
    df.iloc[valid_indexes].plot() 
    

    【讨论】:

    • 第一个建议出现错误:AttributeError: 'numpy.dtype' object has no attribute 'convert_objects' 出现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2022-12-05
    • 2014-07-13
    • 1970-01-01
    • 2015-01-06
    • 2014-05-04
    • 2015-01-03
    相关资源
    最近更新 更多