Articles

numpy - replace()

numpy - replace()


In this numpy tutorial, we will discuss replace() method performed on numpy array.

 

Introduction

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.

Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.

  • replace()

This method will replace the element with the other string in the numpy array

Syntax:

numpy.char.replace(array_input,element/character_in_element,new_string)

where,

  1. array_input is the numpy input array.
  2. element is the existing element in array or character_in_element  is the existing character in the element .
  3. new_string is the strig that replaces the 

 

Example:

In this example, we will create  an numpy array and apply isupper() on the numpy array.

#importing numpy module
import numpy

#create numpy array with 5 string elements
data1=numpy.array(['Php',"HTML","JSP","Go lang",'Python/R'])

#display the array
print(data1)

#create numpy array with 5 string elements
data2=numpy.array(['PHP',"Html90css","Jsp","Go lang",'Python/R'])

#display the array
print(data2)

print()

#check whether first array has upper case elements or not
print(numpy.char.isupper(data1))

#check whether second array has upper case elements or not
print(numpy.char.isupper(data2))

Output:

In first array, the elements - 'HTML' 'JSP' are in upper case, hence it returned True, remaining contains lower case, so they are False.

 

In the second array, only PHP is in upper case. so it returned True, remaining are in lower case.So False is returnrd for them.

['Php' 'HTML' 'JSP' 'Go lang' 'Python/R']
['PHP' 'Html90css' 'Jsp' 'Go lang' 'Python/R']

[False  True  True False False]
[ True False False False False]
  • islower()

This method will check each and every element and returns True if it in lower case. otherwise it will return False.

Syntax:

numpy.char.islower(array_input)

where, array_input is the numpy input array

 

Example:

In this example, we will create  an numpy array and apply islower() on the numpy array.

#importing numpy module
import numpy

#create numpy array with 5 string elements
data1=numpy.array(['Php',"HTML","jsp","Golang",'Python/R'])

#display the array
print(data1)

#create numpy array with 5 string elements
data2=numpy.array(['PHP',"php","Jsp","Golang",'Python/R'])

#display the array
print(data2)

print()

#check whether first array has lower case elements or not
print(numpy.char.islower(data1))

#check whether second array has lower case elements or not
print(numpy.char.islower(data2))

Output:

In first array, jsp element is in lower case, so it returned True, others are all False.

 

In second array, php element is in lower case, so it returned True, others are all False.

['Php' 'HTML' 'jsp' 'Golang' 'Python/R']
['PHP' 'php' 'Jsp' 'Golang' 'Python/R']

[False False  True False False]
[False  True False False False]

 


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!