我有以下数据
+-----------+----+-------+------+-----+----------------+----------+
| ticket_id | id | level | code | qty | AfterDiscTotal | baseCode |
+-----------+----+-------+------+-----+----------------+----------+
| 1 | 0 | 0 | 1710 | 1 | 20 | 0 |
| 1 | 0 | 1 | 372 | 1 | 6 | 1710 |
| 1 | 0 | 2 | 607 | -1 | 0 | 1710 |
| 1 | 1 | 0 | 1727 | 1 | 20 | 0 |
| 1 | 1 | 1 | 370 | 1 | 6 | 1727 |
| 1 | 1 | 2 | 607 | -1 | 0 | 1727 |
| 2 | 0 | 0 | 277 | 1 | 19 | 0 |
| 2 | 0 | 1 | 371 | 1 | 6 | 277 |
| 2 | 0 | 2 | 607 | -1 | 0 | 277 |
+-----------+----+-------+------+-----+----------------+----------+
我需要使用baseCode
删除项目,并将其值添加到code
中相应的AfterDiscTotal
值
我已经写了下面的代码
remove_index= []
for _, group in df_Final.groupby(['ticket_id','id']):
for index, row in group.iterrows():
try:
main_code_data = group[group['baseCode'] == row['code']]
group.at[index, 'AfterDiscTotal'] = row['AfterDiscTotal'] + main_code_data['AfterDiscTotal']
for item in main_code_data.index:
remove_index.append(item)
except:
pass
data = df_Final.drop(remove_index)
我得到上述代码的答案
+-----------+----+-------+------+-----+----------------+----------+
| ticket_id | id | level | code | qty | AfterDiscTotal | baseCode |
+-----------+----+-------+------+-----+----------------+----------+
| 1 | 0 | 0 | 1710 | 1 | 20 | 0 |
| 1 | 1 | 0 | 1727 | 1 | 20 | 0 |
| 2 | 0 | 0 | 277 | 1 | 19 | 0 |
+-----------+----+-------+------+-----+----------------+----------+
但我想要的输出应该是
+-----------+----+-------+------+-----+----------------+----------+
| ticket_id | id | level | code | qty | AfterDiscTotal | baseCode |
+-----------+----+-------+------+-----+----------------+----------+
| 1 | 0 | 0 | 1710 | 1 | 26 | 0 |
| 1 | 1 | 0 | 1727 | 1 | 26 | 0 |
| 2 | 0 | 0 | 277 | 1 | 25 | 0 |
+-----------+----+-------+------+-----+----------------+----------+
我的代码几乎可以正常工作,只是没有对这些值求和。 我的代码中缺少某些内容,我无法弄清楚。