Dataframe Python - Conditional Column based on multiple criteria

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:

The table below is for your reference:

My code is below but it is not working:

Topic dataframe python

Category Data Science


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)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.