replace 'na' with 'nan' so it is then converted to np.nan, then use np.nanmean.

example:

test = np.array(['0','1','nan'], dtype=float)
np.where(np.isnan(test), np.nanmean(test), test)

array([ 0. ,  1. ,  0.5])
Answer from Julien on Stack Overflow
🌐
Scaler
scaler.com › home › topics › convert different python data structures to numpy array?
Convert different Python Data structures to Numpy Array? - Scaler Topics
May 4, 2023 - ... Over here, the string parameter ... using an illustration. ... To convert data from a Dictionary format to a NumPy array format, we utilize np.array() and a list type conversion....
🌐
IncludeHelp
includehelp.com › python › how-to-convert-map-object-to-numpy-array.aspx
Python - How to convert map object to NumPy array?
We can also define the data type of the elements of the numpy array which is going to be created. ... # Import numpy import numpy as np # performing some operation f = lambda x: x**2 # Creating a map object seq = map(f, range(5)) # Display map object print("Map object:\n",seq,"\n") # Converting map object into numpy array arr = np.fromiter(seq,dtype='int64') # Display numpy array print("Converted array:\n",arr)
Top answer
1 of 8
28

Nasty little problem... I have been fooling around with this toy example:

>>> arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
>>> arr
array([['one', [1, 2, 3]],
       ['two', [4, 5, 6]]], dtype=object)

My first guess was:

>>> np.array(arr[:, 1])
array([[1, 2, 3], [4, 5, 6]], dtype=object)

But that keeps the object dtype, so perhaps then:

>>> np.array(arr[:, 1], dtype=float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

You can normally work around this doing the following:

>>> np.array(arr[:, 1], dtype=[('', float)]*3).view(float).reshape(-1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object

Not here though, which was kind of puzzling. Apparently it is the fact that the objects in your array are lists that throws this off, as replacing the lists with tuples works:

>>> np.array([tuple(j) for j in arr[:, 1]],
...          dtype=[('', float)]*3).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Since there doesn't seem to be any entirely satisfactory solution, the easiest is probably to go with:

>>> np.array(list(arr[:, 1]), dtype=float)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Although that will not be very efficient, probably better to go with something like:

>>> np.fromiter((tuple(j) for j in arr[:, 1]), dtype=[('', float)]*3,
...             count=len(arr)).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
2 of 8
13

Based on Jaime's toy example I think you can do this very simply using np.vstack():

arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
float_arr = np.vstack(arr[:, 1]).astype(np.float)

This will work regardless of whether the 'numeric' elements in your object array are 1D numpy arrays, lists or tuples.

Find elsewhere
🌐
YouTube
youtube.com › watch
NumPy Array from Python List | Convert Lists to Arrays Tutorial for Beginners - YouTube
🔢 Learn how to convert Python lists into NumPy arrays in this comprehensive beginner-friendly tutorial!In this video, you'll discover the essential techniqu...
Published: October 28, 2025
🌐
Scaler
scaler.com › home › topics › pandas › converting data into a numpy array
Converting Data into a NumPy Array - Scaler Topics
May 4, 2023 - Even so, we utilize the .head() function to access the first five values of the Weight column in this code. ... To supply the dtype in this example, we are just giving the arguments in the same function. ... To express a given series or data frame object as a Numpy-array, utilize the Pandas Series....
🌐
NumPy
numpy.org › devdocs › user › basics.creation.html
Array creation — NumPy v2.6.dev0 Manual
You can use these methods to create ndarrays or Structured arrays. This document will cover general methods for ndarray creation. NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: ... further nested lists will create higher-dimensional arrays. In general, any array object is called an ndarray in NumPy.
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
Like in above code it shows that arr is numpy.ndarray type. To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_numpy.html
pandas.DataFrame.to_numpy — pandas 3.0.6 documentation
>>> df["C"] = pd.date_range("2000", periods=2) >>> df.to_numpy() array([[1, 3.0, Timestamp('2000-01-01 00:00:00')], [2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object)
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-python-list-to-numpy-arrays
Convert Python List to numpy Arrays - GeeksforGeeks
July 12, 2025 - Let’s now discuss the different ways of converting a list to an array in more detail. The numpy.array() function is the most common way to convert a list into an array.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.5 Manual
By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.
🌐
Towards Data Science
towardsdatascience.com › home › data science › how to convert pandas dataframe into numpy array
How To Convert Pandas DataFrame Into NumPy Array | Towards Data Science
May 6, 2022 - In today's article we discussed ... and to_records() methods of the pandas.DataFrame object as well as the numpy.asarray() method....