NumPy – Arithmetic operations with array containing string elements
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:
import numpy as gfg
a = gfg.array([ 1 , 2 , 3 , 'geeks' , 'for' , 'geeks' , 4 , 5 ])
print (f "Actual array: {a}" )
def m(a):
if a = = 'geeks' or a = = 'for' :
return gfg.nan
else :
return float (a)
b = gfg.frompyfunc(m, 1 , 1 )
a = b(a)
a = a.astype( float )
print (f "Array after changes: {a}" )
m = gfg.nanmean(a)
print (f "Mean of the array: {m}" )
s = gfg.nansum(a)
print (f "Sum of the array: {s}" )
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
Last Updated :
17 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...