How to replace value in specific index in each row with corresponding value in numpy array

Viewed 617

My dataframe looks like this:

     datetime1 datetime2 datetime3 datetime4
id                                      
1    5          6         5         5   
2    7          2         3         5  
3    4          2         3         2 
4    6          4         4         7 
5    7          3         8         9 

and I have a numpy array like this:

index_arr = [3, 2, 0, 1, 2]

This numpy array refers to the index in each row, respectively, that I want to replace. The values I want to use in the replacement are in another numpy array:

replace_arr = [14, 12, 23, 17, 15]

so that the updated dataframe looks like this:

     datetime1 datetime2 datetime3 datetime4
id                                      
1    5          6         5         14   
2    7          2         12        5  
3    23         2         3         2 
4    6          17        4         7 
5    7          3         15        9 

What is the best way to go about doing this replacement quickly? I've tried using enumerate and iterrows but couldn't get the syntax to work. Would appreciate any help - thank you

3 Answers

Here's one way with np.put_along_axis -

In [50]: df
Out[50]: 
   datetime1  datetime2  datetime3  datetime4
1          5          6          5          5
2          7          2          3          5
3          4          2          3          2
4          6          4          4          7
5          7          3          8          9

In [51]: index_arr = np.array([3, 2, 0 ,1 ,2])

In [52]: replace_arr = np.array([14, 12, 23, 17 ,15])

In [53]: np.put_along_axis(df.to_numpy(),index_arr[:,None],replace_arr[:,None],axis=1)

In [54]: df
Out[54]: 
   datetime1  datetime2  datetime3  datetime4
1          5          6          5         14
2          7          2         12          5
3         23          2          3          2
4          6         17          4          7
5          7          3         15          9

IIUC, you can just assign to .values (or .to_numpy(copy=False)):

# <= 0.23
df.values[np.arange(len(df)), index_arr] = replace_arr
# 0.24+
df.to_numpy(copy=False)[np.arange(len(df)), index_arr] = replace_arr
df

    datetime1  datetime2  datetime3  datetime4
id                                            
1           5          6          5         14
2           7          2         12          5
3          23          2          3          2
4           6         17          4          7
5           7          3         15          9

End up using .iat

for x, y ,z in zip(np.arange(len(df)),index_arr ,replace_arr ):
    df.iat[x,y]=z

df
Out[657]: 
    datetime1  datetime2  datetime3  datetime4
id                                            
1           5          6          5         14
2           7          2         12          5
3          23          2          3          2
4           6         17          4          7
5           7          3         15          9
Related