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'
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'.
You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With