使用另一列的总和按另一列分组创建一个新列 - Python Pandas

Create a new colum with the aggregation of the sum of another colum grouped by another column - Python Pandas

我想为后续数据集创建一个新列,它将接收按每个 PNO row/column 值分组的 ForecastSum 列的总和:

所以我想为此 df 创建一个新列“ForecastPNO”,它将作为 PNO 8007205 的示例接收所有行的 ForecastSUM 值 (6 + 7 = 13) 的分组总和,并连续其他 PNO 值,因此每个索引的总和顺序应遵循 PNO 列的顺序:
13/13/13/ 5/5/5 /9/9/9

具有此新列的 table 应具有与共享的 df 数据集图像中相同的索引数。

尝试-

df['new_col'] = df.groupby('PNO')['ForecastSUM'].transform(sum)