I have a dataframe like the one below
d = {"to_explode": [[1, 2, 3], [4, 5], [6, 7, 8, 9]], "numbers": [3, 2, 4]}
df = pd.DataFrame(data=d)
to_explode numbers
0 [1, 2, 3] 3
1 [4, 5] 4
2 [6, 7, 8, 9] 12
I want to call pd.explode
on the list-like column, but I want to divide the data in the other column accordingly.
In this example, the values in the numbers
column for the first row would be replaced with 1
- i.e. 3 / 3 (the corresponding number of items in the to_explode
column).
How would I do this please?