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

后端 未结 2 409
日久生厌
日久生厌 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:25

    What about

    my_array = np.array(map(lambda x: numpy.nan if x==None else x, my_list))
    
    0 讨论(0)
  • 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])
    
    0 讨论(0)
提交回复
热议问题