Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Logic on Pandas DataFrame

Tags:

python

pandas

How to apply conditional logic to a Pandas DataFrame.

See DataFrame shown below,

   data desired_output 0     1          False 1     2          False 2     3           True 3     4           True 

My original data is show in the 'data' column and the desired_output is shown next to it. If the number in 'data' is below 2.5, the desired_output is False.

I could apply a loop and do re-construct the DataFrame... but that would be 'un-pythonic'

like image 453
nitin Avatar asked Feb 05 '13 18:02

nitin


People also ask

How do you use conditionals in pandas?

Let us create a Pandas DataFrame that has 5 numbers (say from 51 to 55). Let us apply IF conditions for the following situation. If the particular number is equal or lower than 53, then assign the value of 'True'. Otherwise, if the number is greater than 53, then assign the value of 'False'.

How do I create a conditional column in pandas?

You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.


1 Answers

In [1]: df Out[1]:    data 0     1 1     2 2     3 3     4 

You want to apply a function that conditionally returns a value based on the selected dataframe column.

In [2]: df['data'].apply(lambda x: 'true' if x <= 2.5 else 'false') Out[2]: 0     true 1     true 2    false 3    false Name: data 

You can then assign that returned column to a new column in your dataframe:

In [3]: df['desired_output'] = df['data'].apply(lambda x: 'true' if x <= 2.5 else 'false')  In [4]: df Out[4]:    data desired_output 0     1           true 1     2           true 2     3          false 3     4          false 
like image 145
Zelazny7 Avatar answered Oct 02 '22 14:10

Zelazny7