matthew matthew - 16 days ago 6
Python Question

Add a new column and insert specific values according to an defined intervall in python

How to add a new column in a pandas dataframe and insert 1 for all values <=W1, 2 for all values <=W2 and 3 for all values >W2 ?

W1=3
W2=6


This is my example case:

column1 number
2 1
1 1
5 2
6 2
7 3
8 3
3 1

Answer

You can double numpy.where:

W1=3
W2=6

df['d'] = np.where(df['column1'] <= W1, 1, 
          np.where(df['column1'] <= W2, 2, 3))
print (df)
   column1  number  d
0        2       1  1
1        1       1  1
2        5       2  2
3        6       2  2
4        7       3  3
5        8       3  3
6        3       1  1

Another solution with cut, docs:

bins = [-np.inf, W1, W2, np.inf]
labels=[1,2,3]
df['d1'] = pd.cut(df['column1'], bins=bins, labels=labels)
print (df)

   column1  number  d d1
0        2       1  1  1
1        1       1  1  1
2        5       2  2  2
3        6       2  2  2
4        7       3  3  3
5        8       3  3  3
6        3       1  1  1