Home > OS >  Pandas: Apportioning parent row value to child rows
Pandas: Apportioning parent row value to child rows

Time:06-30

I have a dataset where the parent rows are followed by child rows. Lets say, the parent row has details of a package and the child rows have details of the products in the package.

enter image description here

The parent row has a price and the child rows have 0 as price. I want a way in pandas to apportion the price of the parent record to the child records. This can be populated in a new column.

There are different packages so there is no fixed window I can use to solve this. Any help on this would be greatly appreciated. The ouput I expect is below,

enter image description here

One way to identify the parent row is it always has the value in column P euqal to the value in column package

CodePudding user response:

You can try groupby and transform

m = df['P'].eq(df['package'])
df['out'] = (df.groupby(m.cumsum())
             ['price'].transform(lambda col: col.item()/(len(col)-1))
             .mask(m, pd.NA))
  • Related