2
$\begingroup$

I want someone to correct my code in python. My goal is to create a code that will add a new column that is based on the conditional function of two columns in my dataframe. I want to add a fees column which is a numeric column that is different and based on whether the success is True or False and based on the PSP column as well.

Note that data type are as below:

success = boolen

PSP = object

My dataframe sample is below: enter image description here

The table below is for your reference: enter image description here

My code is below but it is not working:

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

For apply functions, they pass in the arguments as a series or numpy array. So in general you can modify your function to assume the arguments come in as a list:

# Fake data
name = ['MoneyCard','Goldcard','UK_Card','Simplecard']*2
succ = [True]*4 + [False]*4
df = pd.DataFrame(zip(name,succ),columns=['name','succ'])

# dictionary for success/fail
di_map = {'MoneyCard': [5,2],
          'Goldcard': [10,5],
          'UK_Card': [3,1],
          'Simplecard': [1,0.5]}

# assumes first is name, second is success
def fees(arg):
    if arg[1]:
        return di_map[arg[0]][0]
    else:
        return di_map[arg[0]][1]

# make sure to pass in correct order
df[['name','succ']].apply(fees,axis=1)

There are ultimately many different ways you might do this. You could use merge or replace functions as well. apply is nice as it is more general and can be modified how you want to deal with say missing values or cards not in your list.

Here is another example using dictionaries + replace to accomplish the same end result:

# Another approach
dip = {c:v[1] for c,v in di_map.items()}
din = {c:v[0] for c,v in di_map.items()}
df['fee'] = df['name'].replace(dip)
nfee = df['name'].replace(din)
df['fee'].where(df['succ'], nfee, inplace=True)
$\endgroup$
2
  • $\begingroup$ Dear Andy, Thank you for your support. I would appreciate to know how can I add the column 'succ' in my current df as shown above in the question. $\endgroup$ May 22 at 13:21
  • $\begingroup$ I just shorthand named it succ in my example, it is the same as your success column. Also when accessing multiple columns, you will want to pass in a list, e.g. df[['name','success']]. $\endgroup$
    – Andy W
    May 22 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.