Google News
logo
NumPy - Interview Questions
Shown below is the input NumPy array. Delete column two and replace it with the new column given below.
import NumPy
sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]]) 
newColumn = NumPy.array([[10,10,10]])
 
Expected Output :
 
Printing Original array
[[34 43 73]
 [82 22 12]
 [53 94 66]]

Array after deleting column 2 on axis 1

[[34 73]
[82 12]
[53 66]]

Array after inserting column 2 on axis 1

[[34 10 73]
[82 10 12]
[53 10 66]]
 
Solution :
 
import NumPy
print(“Printing Original array”)
sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]]) 
print (sampleArray)
print(“Array after deleting column 2 on axis 1”)
sampleArray = NumPy.delete(sampleArray , 1, axis = 1) 
print (sampleArray)
arr = NumPy.array([[10,10,10]])
print(“Array after inserting column 2 on axis 1”)
sampleArray = NumPy.insert(sampleArray , 1, arr, axis = 1) 
print (sampleArray)

 

Advertisement