Convert python list with None values to numpy array with nan values

后端 未结 2 410
日久生厌
日久生厌 2021-02-01 16:07

I am trying to convert a list that contains numeric values and None values to numpy.array, such that None is replaces with numpy.nan

2条回答
  •  深忆病人
    2021-02-01 16:30

    You simply have to explicitly declare the data type:

    >>> my_list = [3, 5, 6, None, 6, None]
    >>> np.array(my_list, dtype=np.float)
    array([  3.,   5.,   6.,  nan,   6.,  nan])
    

提交回复
热议问题