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.
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.
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course