pythonpandaspandas-explode

Call pandas explode on one column, and divide the other columns accordingly


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?


Solution

  • You need to perform the computation (get the list length with str.len), then explode:

    out = (df
     .assign(numbers=df['numbers'].div(df['to_explode'].str.len()))
     .explode('to_explode')
    )
    

    output:

      to_explode  numbers
    0          1      1.0
    0          2      1.0
    0          3      1.0
    1          4      1.0
    1          5      1.0
    2          6      1.0
    2          7      1.0
    2          8      1.0
    2          9      1.0