Samat
Samat

Reputation: 43

Splitting value from one row to other rows in the same column in pandas

I have a question regarding to pandas

I have got a DataFrame df

TaskId UserId Hours
123456 123456 19
123456 123456 NaN
123456 123456 NaN
123456 123456 NaN
654321 654321 10

Now I want to split the 19 from the first row into equal amounts where the TaskId and UserId is the same 19 / 4 = 4.75

This is what I would like to receive

TaskId UserId Hours
123456 123456 4.75
123456 123456 4.75
123456 123456 4.75
123456 123456 4.75
... ... ...

I couldn't find anything here on stackoverflow

thank you

Upvotes: 2

Views: 56

Answers (1)

jezrael
jezrael

Reputation: 863611

Use GroupBy.transform for divide first values by counts:

g = df.groupby(['TaskId','UserId'])['Hours']
df['Hours'] = g.transform('first').div(g.transform('size'))
print (df)

   TaskId  UserId  Hours
0  123456  123456   4.75
1  123456  123456   4.75
2  123456  123456   4.75
3  123456  123456   4.75
4  654321  654321  10.00

Upvotes: 2

Related Questions