NumPy - Arithmetic operations with array containing string elements Last Updated : 17 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Numpy is a library of Python for array processing written in C and Python. Computations in numpy are much faster than that of traditional data structures in Python like lists, tuples, dictionaries etc. due to vectorized universal functions. Sometimes while dealing with data, we need to perform arithmetic operations but we are unable to do so because of the presence of unwanted strings in our data. So it is necessary to remove them. Here we are going to create a universal function to replace unwanted strings to NaN. Explanation: Given a numpy array containing some unwanted string. In a user-defined function, unwanted strings are replaced with NaN using conditional statements. numpy.frompyfunc() is used to convert the user-defined function into universal function. The numpy array is then passed to that function, but still, the data type of the array is an object. Therefore we need to convert its datatype to float using array.astype(). It should be noted that NaN values cannot be converted to any other datatype than float. Now we can perform arithmetic operations on it using NaN safe version of inbuilt universal functions. Code: Python3 1== # Importing numpy library import numpy as gfg # Creating array a = gfg.array([1,2,3,'geeks','for','geeks',4,5]) print(f"Actual array: {a}") # Creating universal function to remove unwanted # strings from actual array def m(a): if a == 'geeks'or a=='for': return gfg.nan else: return float(a) # Converting user-defined function to universal function b = gfg.frompyfunc(m,1,1) # Calling function a = b(a) # Changing datatype of array a = a.astype(float) print(f"Array after changes: {a}") # Calculating mean of the array m = gfg.nanmean(a) print(f"Mean of the array: {m}") # Calculating sum of the array s = gfg.nansum(a) print(f"Sum of the array: {s}") # Calculating product of the array p = gfg.nanprod(a) print(f"Product of the array: {p}") Output: Actual array: ['1' '2' '3' 'geeks' 'for' 'geeks' '4' '5'] Array after changes: [ 1. 2. 3. nan nan nan 4. 5.] Mean of the array: 3.0 Sum of the array: 15.0 Product of the array: 120.0 Comment More infoAdvertise with us Next Article Numpy Step By Step Guide H hacksight Follow Improve Article Tags : Numpy Similar Reads Find the length of each string element in the Numpy array NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). In this post 3 min read Numpy string operations | rindex() function numpy.core.defchararray.rindex() function, raises ValueError when the substring sub is not found. Calls str.rindex element-wise. Syntax : numpy.core.defchararray.rindex(arr, sub, start = 0, end = None) Parameters : arr : [array-like of str or unicode] Array-like of str . sub : [str or unicode] Input 1 min read NumPy Tutorial - Python Library NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens 3 min read Numpy Step By Step Guide NumPy is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPyâs array objects are more memory-efficient and perform better than Python lists, whi 6 min read How to Take Array Input in Python Using NumPy NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array. Taking Array Input Using numpy.array()The most simple way to create 3 min read Python program to compute arithmetic operation from String Given a String with the multiplication of elements, convert to the summation of these multiplications. Input : test_str = '5x10, 9x10, 7x8' Output : 196 Explanation : 50 + 90 + 56 = 196. Input : test_str = '5x10, 9x10' Output : 140 Explanation : 50 + 90 = 140. Method 1 : Using map() + mul + sum() + 4 min read Like