When using df1[~df1.isin(df2)].dropna() Question

Try this:

invoices = pd.DataFrame([['111', '2g', 53],
                         ['112', '7g', 25],
                         ['112', '7g', 25],
                         ['113', '8g', 20],
                         ['113', '8g', -20],
                         ['114', '9g', 15],
                         ['115', '2g', 53],
                         ['115', '2g', 53],
                         ['115', '2g', -53]],
                        columns=['Case', 'PartNo', 'Cost'])

print(f"Original invoices:\n{invoices}\n\n")

newInvoices = invoices.copy()
newInvoices['Charge_Credit'] = 0

for idx, case, part, cost, ch_cr in newInvoices.itertuples():
    creditedDf = newInvoices[(newInvoices.Case == case) &
                          (newInvoices.PartNo == part) &
                          (newInvoices.Cost == -cost) &
                          (newInvoices.Charge_Credit != 'remove')]
    if len(creditedDf):
        newInvoices.loc[creditedDf.iloc[0].name,  'Charge_Credit'] = 'remove'

newInvoices = newInvoices[['Case', 'PartNo', 'Cost']][newInvoices.Charge_Credit != 'remove']
newInvoices.reset_index(drop=True, inplace=True)
print(f"New invoices:\n{newInvoices}\n")

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top