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

How to create column with the numbers 0 or 1 when substraction two columns in the same dataframe is less than zero?

In a simple example, I have a dataframe that looks like this (I am going to put in a dict structure, but it is really a dataframe):

data = {'value': [1,2,3,4,5,1,3,4,6],
        'limit': [4,4,4,4,3,3,3,1,1],
        }
data = pd.Dataframe(data)

I need add an extra column to dataframe that look like this:

data = {'value': [1, 2, 3, 4, 5, 1, 3, 4, 6],
        'limit': [4, 4, 4, 4, 3, 3, 3, 1, 1],
        'criteria': [0, 0, 0, 0, 1, 0, 0, 1, 1]
        }

    ''' The criteria is determined as follows (example):
    Looking at the row zero: 
value is 1. 
limit is 4. 
diff is -3. 
Given that -3 < 0 (less then 0). 
Criteria is 0.
    
    Looking at the row 4: 
value is 5. 
limit is 3. 
diff is 2 (higher than 0). 
Criteria is 1.'''


data['criteria'] = data[((data['value'] - df1['limit']) > 0) == 1]

Nevertheless, it does not work because I am not really assigning any values to the criteria column.

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

>Solution :

A possible solution, which uses numpy.where:

data['criteria'] = np.where((data['value'] - data['limit']) > 0, 1, 0)

Output:

   value  limit  criteria
0      1      4         0
1      2      4         0
2      3      4         0
3      4      4         0
4      5      3         1
5      1      3         0
6      3      3         0
7      4      1         1
8      6      1         1
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