Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

if condition dataframe python

df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Comment

if else pandas dataframe

import pandas as pd

numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(numbers,columns=['set_of_numbers'])

df.loc[df['set_of_numbers'] <= 4, 'equal_or_lower_than_4?'] = 'True' 
df.loc[df['set_of_numbers'] > 4, 'equal_or_lower_than_4?'] = 'False' 

print (df)
Comment

python pandas if statement

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
Comment

PREVIOUS NEXT
Code Example
Python :: select rows with multiple conditions pandas query 
Python :: pandas read csv python 
Python :: python doctype 
Python :: get first element of array python 
Python :: beautifulsoup get h1 
Python :: drf serializer general validate method 
Python :: dataframe summarize how many in each column 
Python :: python keyboard hold key 
Python :: python list as queue 
Python :: Remove whitespace from str 
Python :: code for test and train split 
Python :: pandas dataframe caption 
Python :: rename a file in python 
Python :: compare lists element wise python 
Python :: __mul__ 
Python :: numpy arange float step 
Python :: isnumeric() in python 
Python :: SUMOFPROD1 Solution 
Python :: python dictionary key in range 
Python :: python regex match 
Python :: to_frame pandas 
Python :: python random uuid 
Python :: convert int to float python 
Python :: python print variable 
Python :: python string to tuple 
Python :: django fixtures. To loaddata 
Python :: remove all parentheses from string python 
Python :: To Divide or Not To Divide codechef solution 
Python :: sep and end in print python 
Python :: how to check uppercase in python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =