Python 如果数据帧满足两个不同的要求,则将两行相加
我有下表Python 如果数据帧满足两个不同的要求,则将两行相加,python,pandas,sum,rows,Python,Pandas,Sum,Rows,我有下表 | product | check | check1 | type | amount | |---------|-------|--------|------|--------| | A | 1 | a | c | -10 | | A | 1 | a | p | 20 | | B | 2 | b | c | 20 | | B | 2 |
| product | check | check1 | type | amount |
|---------|-------|--------|------|--------|
| A | 1 | a | c | -10 |
| A | 1 | a | p | 20 |
| B | 2 | b | c | 20 |
| B | 2 | b | p | 20 |
| C | 3 | c | c | -10 |
| D | 4 | d | p | 15 |
| D | 4 | d | c | -15 |
我想对前三列相等且“类型”列中的一行包含“C”,而另一行包含“p”的行的金额求和,然后对其中的“类型”=“C”金额应为负值,当“类型”=“p”时,金额应为正值,否则不应求和。如果将它们相加,则如果“金额”为负数,“类型”应为“c”,否则为“p”。请参阅以下所需输出:
| product | check | check1 | type | amount |
|---------|-------|--------|------|--------|
| A | 1 | a | p | 10 |
| B | 2 | b | c | 20 |
| B | 2 | b | p | 20 |
| C | 3 | c | c | -10 |
| D | 4 | d | p | 0 |
我在前三列中尝试了group.by
,然后应用lambda函数
df = df.groupby(['product', 'check', 'check1']).apply(lambda x, y : x + y, x.loc[(x['type']=='c')], y.loc[(y['type']=='p')], 'amount')
这会在未定义“x”的情况下产生名称错误。我也不确定这是不是正确的方式,所以如果你有任何提示,请让我知道 这里有一个解决方案,可能效率不高,但它确实有效
new_df=pd.DataFrame()
对于df['product'].unique()中的产品:
对于签入df[df['product']==product].check.unique():
对于df[(df['product']==product)和(df.check==check)]中的check1.unique():
tmp=df[(df['product']==product)&(df.check==check)&(df.check1==check1)]
如果len(tmp[((tmp.type='c')&(tmp.amount<0))|((tmp.type='p')&(tmp.amount>0))!=2:
new_df=new_df.append(tmp,ignore_index=True)
其他:
amount=tmp.sum()['amount']
类型='c'如果金额<0,则为'p'
英语教学={
“产品”:产品,
“检查”:检查,
“check1”:check1,
“类型”:类型,
“金额”:金额
}
new_df=new_df.append(pd.Series(elt),ignore_index=True)
这里有一个解决方案,可能效率不高,但它确实有效
new_df=pd.DataFrame()
对于df['product'].unique()中的产品:
对于签入df[df['product']==product].check.unique():
对于df[(df['product']==product)和(df.check==check)]中的check1.unique():
tmp=df[(df['product']==product)&(df.check==check)&(df.check1==check1)]
如果len(tmp[((tmp.type='c')&(tmp.amount<0))|((tmp.type='p')&(tmp.amount>0))!=2:
new_df=new_df.append(tmp,ignore_index=True)
其他:
amount=tmp.sum()['amount']
类型='c'如果金额<0,则为'p'
英语教学={
“产品”:产品,
“检查”:检查,
“check1”:check1,
“类型”:类型,
“金额”:金额
}
new_df=new_df.append(pd.Series(elt),ignore_index=True)
谢谢!这是完美的,也许不是最有效的方式,但我已经很高兴这是有效的,已经挣扎了好几天了!谢谢这是完美的,也许不是最有效的方式,但我已经很高兴这是有效的,已经挣扎了好几天了!