use if in pandas code example
Example 1: if condition dataframe python
df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Example 2: pandas if python
import pandas as pd
names = {'First_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['First_name'])
df.loc[(df['First_name'] == 'Bill') | (df['First_name'] == 'Emma'), 'name_match'] = 'Match'
df.loc[(df['First_name'] != 'Bill') & (df['First_name'] != 'Emma'), 'name_match'] = 'Mismatch'
print (df)
Example 3: pandas if python
df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')