简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM