[英]Splitting value from one row to other rows in the same column in pandas
I have a question regarding to pandas我有一个关于 pandas 的问题
I have got a DataFrame df我有一个 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现在我想将第一行中的 19 分成相等的数量,其中 TaskId 和 UserId 相同 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我在stackoverflow上找不到任何东西
thank you谢谢你
Use GroupBy.transform
for divide first values by counts:使用
GroupBy.transform
将第一个值除以计数:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.