Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 449
matthew Avatar asked Dec 04 '22 23:12

matthew


2 Answers

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
like image 131
jezrael Avatar answered Mar 12 '23 20:03

jezrael


df['new'] = df.column1.gt(W1).add(1).add(df.column1.gt(W2))

df

enter image description here


When column1 is greater than W1, We get True. Less than or equal gets False. When I add 1, Those boolean values get cast to the integer values 1 and 0 respectively. So the result is 2 and 1 for True and False (because I added 1). So, as of now, I have 1 for Less than or equal to W1 and 2 for greater than W1. I finish it up by adding the boolean series of when column1 is greater than W2 which adds 0 if less than or equal to W2 and adds 1 to the 2's when column1 is greater than W2.

I can show it like this to make it more obvious what it's doing

c = df.column1
(c > W1) + 1 + (c > W2)

0    1
1    1
2    2
3    2
4    3
5    3
6    1
Name: column1, dtype: int64
like image 40
piRSquared Avatar answered Mar 12 '23 20:03

piRSquared