python - 删除负数和相应的正数 - Python
问题描述
我有付款表,并且可能会错误地提交付款,因此出于分析目的,我需要删除否定金额和实际付款(它是在否定金额之前提交的付款)。但是,负金额不一定会转为正金额。
用例 1:原始表:
预期结果:
但是,当几次付款错误完成时,我也有如下情况:用例 2:原始表。虽然,我几乎准备放弃这些用例。
预期结果:
我试图在 SQL 级别解决它,但这似乎是 SQL 的一项艰巨任务。我尝试过的所有 SQL 解决方案,取金额的模数,然后对 accountid 和 abs 值进行几组,我觉得这是正确的方向,但是,也许在 Python 世界中有更好的方法来做到这一点。
import numpy as np
import pandas as pd
data = {
'id': ['1','2','3','4','5','6','7','8','9','10'],
'accountid': ['1','1','1','1','1','2','2','2','2','2'],
'amount': [5,5,5,5,-5,5,5,5,-15,5]
}
df = pd.DataFrame(data)
解决方案
import numpy as np
import pandas as pd
data = {
'id': ['1','2','3','4','5','6','7','8','9','10'],
'accountid': ['1','1','1','1','1','2','2','2','2','2'],
'amount': [5,5,5,5,-5,5,5,5,-15,5]
}
df = pd.DataFrame(data)
def clean_df(df):
df = df.copy()
result = []
g = df.groupby("accountid")
for _, group in g:
to_reduce = group[group.amount < 0].amount.abs().sum()
to_drop = []
group = group[group.amount > 0]
for index, row in group[::-1].iterrows():
if to_reduce > 0:
if row.amount > to_reduce:
# update row in gorup
row.amount -= to_reduce
group.loc[group.index == index, "amount"] = row.amount
to_reduce = 0
else:
to_reduce -= row.amount
to_drop.append(index)
if to_reduce == 0:
group = group.drop(to_drop)
result.append(group)
break
return pd.concat(result)
clean_df(df)
# output
id accountid amount
0 1 1 5
1 2 1 5
2 3 1 5
5 6 2 5
只有 accountid == 2 的情况:
id accountid amount
0 1 2 5
1 2 2 5
2 3 2 5
3 4 2 -15
4 5 2 5
clean_df(df)
# oudput
id accountid amount
0 1 2 5
推荐阅读
- ruby-on-rails - Rails 5.2.x,从 CDN 更新为 FontAwesome 套件 - 我需要向我的 CSP 添加什么以允许 css 和字体?
- javascript - 如何克服异步 useState 钩子?
- python - 将基于重复值的 Dataframe 拆分为多个 csv 文件
- javascript - Firebase:为什么在登录后调用`signOut()`?
- html - 如何根据前面的文本匹配标签?
- javascript - 数组元素的增量幂
- flutter - 颤振:最大长度不适用于文本字段
- javascript - 如何将 javascript 函数分配给用 php 呈现的制表符格式化程序?
- go - Go 返回错误以及函数的接口
- java - 在 Java 中设置分辨率大小