0

Help please!!

I was trying to create a column 'Segment' based on the condition:

 if 'Pro_vol' >1 and 'Cost' >=43 then append 1 
 if 'Pro_vol' ==1 and 'Cost' >=33 then append 1 
 or append 0

Below is the code for data:

df = pd.DataFrame({'ID':[1,2,3,4,5,6,7,8,9,10],
            'Pro_vol':[1,2,3,1,5,1,2,1,4,5],
              'Cost' : [12.34,13.55,34.00, 19.15,13.22,22.34,33.55,44.00, 29.15,53.22]})

I tried a code:

Segment=[]

for i in df['Pro_vol']:
if i >1:
    Segment.append(1)
    for j in df['Cost']:
        if j>=43:
            Segment.append(1)
elif i==1:
    Segment.append(1)
elif j>=33:
    Segment.append(1)
else:
    Segment.append(0)

df['Segment']=Segment

And it was giving me an error:

ValueError: Length of values does not match length of index

I don't know any other way to try to find an answer!!

1 Answer 1

1

You may consider np.where

np.where(((df.Cost>=33)&(df.Pro_vol==1))|((df.Cost>=43)&(df.Pro_vol>1)),1,0)
Out[538]: array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.