I have a column of a dataframe with values for customer 'Length of Relationship'. I want to convert these values to numbers from 1-4 based on if they are below average for terminated relationship length, above average, above 1 standard deviation, and above 2 standard deviations. Without looping through using a for loop, is there an easier/faster way to do this?
Here is my code so far:
average = terminatedDf['Relationship Length'].mean()
standardDeviation = terminatedDf['Relationship Length'].std()
lorScores = {np.arange(0, average): 1, np.arange(average, standardDeviation): 2, np.arange(standardDeviation, standardDeviation*2): 3, np.arange(standardDeviation*2, 150): 4}
reportDf['Length of Relationship Score'] = reportDf['Relationship Length'].map(lorScores)
My problem is that numpy arrays are not hashable, but using the regular range function only allows for integers.
I suppose I could loop through the dataframe given that it is only ~ 1500 rows like this:
for row in reportDf:
if row[5] < average:
row[15] = 1
else:
....
I'm not sure even if I got the dictionary to work that .map would be any more efficient than the for loop. Is there a better way to do this? My intuition tells me this may just be an inefficient task to begin with. Here's a sample of what the Dataframe looks like, but it is actually downloaded from a salesforce API.
reportDf = ({'Owner': ['Bob', 'Jane', 'Alice', 'Fred'],
'Name': ['John', 'Johnny', 'Suzie', 'Larry']
'Relationship Length': [0.78, 0.73, 19.36, 7.36]
})
Average length is about 6.3 and standard deviation is about 3.4