The problem is as follows.
I have a pandas dataFrame looking something like
row_idx clm_idx value
0 0 1 a1
1 0 2 b1
2 1 3 c1
3 2 3 d1
This dataFrame can have up to m lines (probably quite a lot).
Secondly I have a nxn numpy array A where I need to add those values to depending on the columns 'row_idx' and 'clm_idx' which describe the row and column index in the numpy array. The mathematical function can even be more complex than simply adding. But I set that aside for now.
A = [[a b c d],
[e f g h],
[i k l m],
[n o p q]]
Here as an example its a 4x4 matrix. So I would like to get the following in the end:
A_new = [[a b+a1 c+b1 d],
[e f g h+c1],
[i k l m+d1],
[n o p q]]
I assume I can iterate over all rows of the dataframe somehow extract the indices and then add the values at the corresponding index to the nxn array. But that seems somewhat inefficient.
I tried as well:
df.apply(lambda x: A[x['row_idx'], x['clm_idx']] += x['value'])
But this throws a SyntaxError that one cannot contain an assignment in this context.
Is there an efficient way to solve the problem, assuming that there m and n are quite big?
As a basic code block this should do. The problem is the last line:
import numpy as np
import pandas as pd
data = {'row_idx': [0, 0, 1, 2], 'clm_idx': [1,2,3,3], 'value': [1,2,3,4]}
df = pd.DataFrame(data)
A = np.zeros((4,4))
df.apply(lambda x: A[x['row_idx'], x['clm_idx']] += x['value'], axis=1)