# How to loop through multiple dataframes with conditions

I need help with adding some conditions to my loop, or an easier way to do these calculations.

Dataframe 1

``````cust_id | current_state | year  | amount
ax111   |   A           |   3   |  300
ax112   |   D           |   4   |  4890
ax113   |   G           |   9   |  624
``````

I have about 300000+ customers that I need to multiply by matrices. `Matrices` is a multi-dimensional array consisting of States A-J (10 transition states) ,and 60 years. For this example `matrices = np.random.rand(60, 10, 10)`.Each of the matrices are in order(A-J) and represent time in years in increments of 1. Starting from matrices which contains the matrix values for year 1, up to matrices year 60.

`cust_amt` - is a dataframe i created for the calculation which basically matches each customers year to the correct matrix and places their amount in their current_state creating a vector for each customer. Example:

`ax111 = np.array([300,0,0,0,0,0,0,0,0,0]) (amount 300 placed at state A, 1st element)`

`ax112 = np([0,0,0,4890,0,0,0,0,0,0]) (amount 4890 placed at state D, 4th element)`

I then need to multiply each customers array by the 2d array list matrices, based on the customers year, and continue multiplying the product by the next matrix until year 60, matrices is reached for each customer.The loop currently works but I need to add a few conditions inbetween.

``````results_dict = {}
for _id, c, y in zip(cust_id ,cust_amt, year):
results = []
for m in matrices[y:]:
c = c @ m
results.append(c)
results_dict[_id] = results
``````

``````year | Limit %
1    | 0.19
2    | 0.32
3    | 0.11
60   | 0.13
``````

I need to edit the loop to include dataframe 2 conditions, in a top-down approach for each user.

Example: cust_id `ax111` I need to multiply by matrices year 3 and then use the answer from the matrix and multiply by limit % (dataframe 2) at year 3, Then I need to multiply the answer by matrices year 4 and multiply by limit % at year 4, and so on for each customer until year 60.How can I add this additional condition to my loop?

Example of output

``````{ax111: [array([    8.2,    49.2,  778.9 ,   246.2 ,   147.6,   237.8,   631.4, 98.4,   303.4,   475.6,]),
array([  385.2,   552.07, 700.2 , 796.3 ,  804.5, 307.5,  1008.2,   598.87,   731.8 ,   190.7 ]),...

``````

Is there a better way of doing this example without loops?