I am trying to score customer data based on how complete their address is. The conditions are as follows:
If address 1 & address 2 are both present: score is 10
If other address details are present: a score of 5 for each one.
The problem is the score output is wrong. It's 15 for both customers, but the first one should be 25, second one should be 15.
Here's a mock dataframe:
import numpy as np
import pandas as pd
df = pd.DataFrame({'address1':['foo','bar'],'address2':['foo','bar'],'city':['foo',np.nan],'country':['hello','bar'],'county':['foo','bar'],'should_be':['25','20']})
df
address1 address2 city country county should_be
0 foo foo foo hello foo 25
1 bar bar NaN bar bar 20
So then I apply the function, which tallies what address info is there/missing, and adds a score in an extra column:
def f(row):
score = 0
# if we have address1 and address2
if (row['address1'] ==True) & (row['address2']==True) ==2:
score += 10
# if we have city data
if row['city']:
score += 5
# if we have country data
if row['country']:
score += 5
# if we have county data
if row['county']:
score += 5
return score
score = 0
df['actual'] = df.apply(f, axis=1)
df
address1 address2 city country county should_be actual
0 foo foo foo hello foo 25 15
1 bar bar NaN bar bar 20 15
First of all, the way I'm doing this is quite messy. Is there a more efficient way of scoring/checking the data?
Secondly, as you can see, both customers were scored 15, when the first one should have a score of 25 due to all address details being present, and the second one should have 20.
I expected the score to be reset to zero after the below section. It's also not adding up the scores correctly. Where am I going wrong?
return score
score = 0
If anybody can spot where I'm going wrong I'd appreciate it. Also, if you have any advice to clean up or make this code more readable I'm all ears. Thank you.