1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.