1

I have the following Pandas dataframe

  df= SlNo Size 
       1     2     
       2     3
       3     1
       4     4

I have created a second column- Size cluster based on whether the attribute is less than 2, equal to 2 or greater than 2

    df[['attribute']]=0

i want to assign values to the attribute column so that values less than 2 are given V1, equal to 2 are given V2, and greater than 2 are given V3.

      SlNo Size attribute
       1     2    V2 
       2     3    V3
       3     1    V1
       4     4    V3

I have tried the following loop

  if df.Size<=1:
 df.attribute="V1"
 elif df.Size<=2 & df.Size>1:
    df.attribute="V2"
 else df.attribute= "V3"

This loop is not able to do the job. I am requesting some help here

3 Answers 3

3

Use cut, advantage is categorical column for save memory and easy add new bins:

df['attribute'] = pd.cut(df['Size'], bins=[-np.inf,1,2, np.inf], labels=['V1','V2', 'V3'])
print (df)
   SlNo  Size attribute
0     1     2        V2
1     2     3        V3
2     3     1        V1
3     4     4        V3

print (df['attribute'])
0    V2
1    V3
2    V1
3    V3
Name: attribute, dtype: category
Categories (3, object): [V1 < V2 < V3]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Its quite generic in that u can use values which may vary also. One need not hard code the values
@Raghavanvmvs - Exactly ;) Thank you and nice day!
3
df['attribute'] = df['Size'].apply(lambda x: 'V1' if x<2 else 'V2' if x==2 else 'V3')

Comments

1

You can define your function:

def myFun(row):
    if row['Size']<2: return 'V1'
    elif row['Size']==2: return 'V2'
    else: return 'V3'

and apply your function:

df.loc[:, 'attribute']=df.apply(myFun, axis=1)

Then:

print(df)

Output:

   Size attribute
0     2        V2
1     3        V3
2     1        V1
3     4        V3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.