scapula13
scapula13

Reputation: 50

How can i split a column into multiple columns?

I have a data like this:

d1 = pd.DataFrame({"Mother_id": 11111, "Children_id": [12476, 19684]})
d2 = pd.DataFrame({"Mother_id": 22222, "Children_id": [24153, 29654, 25417]})

d3 = pd.concat([d1, d2], axis=0)

Desired Output:

    Mother_id   child_id_1  child_2 child_3 ....  number_of_children
(11111, 12476, 19684, nan, 2)
(22222, 24153, 29654, 25417, 3)

Upvotes: 1

Views: 80

Answers (1)

mozway
mozway

Reputation: 260360

Here is a solution using pivot. It first uses groupby+cumcount to compute a helper column with the children's rank that will be used to define the columns for the pivot.

(d3.assign(n=d3.groupby('Mother_id').cumcount().add(1))
   .pivot(index='Mother_id', columns='n', values='Children_id')
   .add_prefix('child_')
   .assign(n_children=lambda d: d.notna().sum(axis=1))
)

output:

           child_1  child_2  child_3  n_children
Mother_id                                       
11111      12476.0  19684.0      NaN           2
22222      24153.0  29654.0  25417.0           3

Upvotes: 2

Related Questions