Skip to content Skip to sidebar Skip to footer

Negate The Values Based On Indicator

i want to add negate the values of Income when D is found # Preparing data # Note that I have clean up the `jan` to `Jan` and `Charge` to `charge` [In] data = [{'Month': 'Jan', 'Na

Solution 1:

After change the value by using condition , that is just a pivot problem

df.loc[df['d/k']=='d','Income']=df.Income*-1

df.pivot_table(index=['id','Name'],columns=['Months','Ex/Im','Info'],values='Income',aggfunc='sum')
Out[423]: 
Months          Jan       
Ex/Im        Import export
Info         charge charge
id  Name                  
212 Alicesal    210    110

Solution 2:

Everything is correct, except your data and the negate part:

data = [
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'export', 'Income': 200},
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'export',  'Income': 10},
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'export', 'Income': 100},
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'Import', 'Income': 400},
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'Import',  'Income': 10},
    {'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'Import', 'Income': 200}]

  • You have month as Feb in data but all records from table view are Jan.
  • Ex/Im column missing in some records
  • Since the negate part is compare against D, you should either change the D to d, or change the value in d/k column to D

Post a Comment for "Negate The Values Based On Indicator"