Numpy recarray.put() function | Python

In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record arrays allow the fields to be accessed as members of the array, using arr.a and arr.b.

numpy.recarray.put() function Replaces specific elements of an record array with given values . The indexing works on the flattened target array.

Syntax : numpy.recarray.put(indices, values, mode='raise')



Parameters:
indices : [array_like] Target indices, interpreted as integers.
values : [array_like] Values to place in a at target indices. If values is shorter than ind it will be repeated as necessary.
mode : [‘raise’, ‘wrap’, ‘clip’, optional] Specifies how out-of-bounds indices will behave.

Return : [ndarray] Resultant array.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.recarray.put() method 
  
# importing numpy as geek
import numpy as geek
  
# creating input array with 2 different field 
in_arr = geek.array([(1.0, 2), (3.0, -4), (5.0, 6),
                     (7.0, 8), (9.0, -4), (11.0, -2)],
                     dtype =[('a', float), ('b', int)])
  
print ("Input array : ", in_arr)
  
# convert it to a record array,
# using arr.view(np.recarray)
rec_arr = in_arr.view(geek.recarray)
print("Record array of float: ", rec_arr.a)
print("Record array of int: ", rec_arr.b)
  
# applying recarray.put methods
# to float record array in default mode
rec_arr.a.put( [0, 2], [-14, 15])
print ("Output float array in default mode : ", rec_arr.a) 
  
# applying recarray.put methods
# to float record array in clip mode
rec_arr.a.put( 13, -4, mode ='clip')
print ("Output  float array in clip mode : ", rec_arr.a) 
  
# applying recarray.put methods 
# to int record array 
rec_arr.b.put([1, 2, 4], [10, 15, 20])
print ("Output int array in default mode : ", rec_arr.b) 
  
# applying recarray.put methods
# to int record array in clip mode
rec_arr.b.put(8, 100, mode ='clip')
print ("Output  int array in clip mode : ", rec_arr.b) 

chevron_right


Output:

Input array :  [( 1.,  2) ( 3., -4) ( 5.,  6) ( 7.,  8) ( 9., -4) (11., -2)]
Record array of float:  [ 1.  3.  5.  7.  9. 11.]
Record array of int:  [ 2 -4  6  8 -4 -2]
Output float array in default mode :  [-14.   3.  15.   7.   9.  11.]
Output  float array in clip mode :  [-14.   3.  15.   7.   9.  -4.]
Output int array in default mode :  [ 2 10 15  8 20 -2]
Output  int array in clip mode :  [  2  10  15   8  20 100]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.