How can I divide a list of dataframes by a list of integers?

2466 views python
-1

(1) I have a list of 29 dataframes

(2) I have a list of 29 integers

I want to divide each in the list from (1) with each in values of (2) -- they are matched by index in their respective lists

So for example, I want the 6th dataframe (all values) in the first list to be divided by the 6th integer in my second list. E.g.:

    print(lst[6]):
     q9036
2.0      5
1.0      4
3.0      3
5.0      2

Divided by 21:

print(tot_qs[6]):
array([[14]], dtype=int64

So the result should look something like this

     q9036   total     result
2.0      5      14      0.357      
1.0      4      14      0.285
3.0      3      14      0.214
5.0      2      14      0.143

I want to do this for all 29 dataframes for all 29 integers

answered question

1 Answer

13

Use:

dfs = [df.assign(total = i[0][0], result = df / i[0][0]) for i, df in zip(L2, L1)]

posted this

Have an answer?

JD

Please login first before posting an answer.