Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate int variable to do in operator check

I already referred the post here and here. Please don’t mark as duplicate. My python version is 3.8.8

I have a dataframe like as below

customer_id   r  f  m  h  y  
   1          4  3  3  3  3    
   2          5  4  2  1  4
   3          3  1  1  1  1
   4          2  2  2  2  2

Based on the code snippet found in this repository here, am trying to do the below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a) Assign a segment_label to each customer based on their r, f, m, h , y values

So, I was working on the below code. But the problem is am getting an error in the 1st if-clause. I already tried replacing in keyword with == symbol. But it doesn’t do the check and ends up in else clause for all records/customers

classes = []
classes_append = classes.append
cust = 'unique_key'
for row in df.iterrows():
    rec = row[1]
    print(rec)
    r = rec['r']
    f = rec['f']
    y = rec['y']
    p = rec['h']
    if (r in (4)) and (f in (4)) and (y in (2,3)) and (h in (3)): # I replaced `in` keyword with `==` symbol as well. The code works but it doesn't check the condition. So, ends up in else clause.
        classes_append({rec[cust]:'Champions'})
    elif (r in (4)) and (f in (4)) and (y in (1,)) and (h in (2,3)):
        classes_append({rec[cust]:'Short Tenure - Promising'})
    elif (r in (3,4)) and (f in (3,4)) and (y in (3,)) and (h in (1,2,3)):
        classes_append({rec[cust]:'Loyal Customers'})
    elif (r in (3,4,5)) and (f in (3,4,5)) and (y in (2,)) and (h in (1,2)):
        classes_append({rec[cust]:'Potential Loyalist'})
    elif (r in (3,4)) and (f in (3,4)) and (y in (1,)) and (h in (1)):
        classes_append({rec[cust]:'Short Tenure - Average'})
    elif (r in (2,)) and (f in (1,2,3)) and (y in (1,2,3)):
        classes_append({rec[cust]:'Needs Attention'})
    elif (r in (4)) and (f in (1,2)) and (y in (1,2,3)):
        classes_append({rec[cust]:'Occasional and New Customers'})
    elif (r in (1,)) and (y in (1,2,3)):
        classes_append({rec[cust]:"Lost"})
    else:
        print("hi")
        classes.append({0:[row[1]['r'],row[1]['f'],row[1]['m']]})
    accs = [list(i.keys())[0] for i in classes]
    segments = [list(i.values())[0] for i in classes]
    df['segment_label'] = df[cust].map(dict(zip(accs,segments)))

The error is

enter image description here

>Solution :

I think the problem is that (4) does not define a tuple with single item apparently hence you get TypeError: 'int' object is not iterable. That’s why it stops at the line

if (r in (4)) and (f in (4)) and (y in (2,3)) and (h in (3)):

Maybe try replacing tuples with lists as follows (e.g. here list of one item):

>>> r = 5
>>> r in [4]
False

so your first if statement will become

if (r in [4]) and (f in [4]) and (y in [2,3]) and (h in [3]):

Update: as The Great suggested in comments adding comma to single item tuple solved the problem. Perhaps that should be the correct answer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading