我需要根据一定的标准进行求和.我相信我需要分组,但我得到一个错误时,应用该方法



我有一个表,它有以下类型的数据:

33

如果您想在原始数据框中添加新列,请使用transform

df1['combinedValue'] =  df1.groupby(['id','approach','start_time'], as_index=False)['value'].transform("sum")
>>> df1
id              start_time    approach movement  value  combinedValue
0   1  11/12/2020 12:00:00 AM  Southbound    Right      2              9
1   2  11/12/2020 12:00:00 AM  Northbound    Right      2             16
2   3  11/12/2020 12:00:00 AM   Eastbound    Right      3             19
3   1  11/12/2020 12:00:00 AM  Southbound     Thru      3              9
4   2  11/12/2020 12:00:00 AM  Northbound     Thru      6             16
5   3  11/12/2020 12:00:00 AM   Eastbound     Thru      7             19
6   1  11/12/2020 12:00:00 AM  Southbound     Left      4              9
7   2  11/12/2020 12:00:00 AM  Northbound     Left      8             16
8   3  11/12/2020 12:00:00 AM   Eastbound     Left      9             19

使用sum而不使用transform,为['id','approach','start_time']的每个唯一组合提供一行。

df2 = df1.groupby(['id','approach','start_time'], as_index=False)['value'].sum()
>>> df2
id    approach              start_time  value
0   1  Southbound  11/12/2020 12:00:00 AM      9
1   2  Northbound  11/12/2020 12:00:00 AM     16
2   3   Eastbound  11/12/2020 12:00:00 AM     19

相关内容

  • 没有找到相关文章

最新更新