![](/img/trans.png)
[英]Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column?
[英]Splitting value from one row to other rows in the same column in pandas
我有一个关于 pandas 的问题
我有一个 DataFrame df
任务 ID | 用户身份 | 小时 |
---|---|---|
123456 | 123456 | 19 |
123456 | 123456 | 钠 |
123456 | 123456 | 钠 |
123456 | 123456 | 钠 |
654321 | 654321 | 10 |
现在我想将第一行中的 19 分成相等的数量,其中 TaskId 和 UserId 相同 19 / 4 = 4.75
这是我想收到的
任务 ID | 用户身份 | 小时 |
---|---|---|
123456 | 123456 | 4.75 |
123456 | 123456 | 4.75 |
123456 | 123456 | 4.75 |
123456 | 123456 | 4.75 |
... | ... | ... |
我在stackoverflow上找不到任何东西
谢谢你
使用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.