Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

if condition dataframe python

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

pandas if python

import pandas as pd

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

df.loc[df['set_of_numbers'] == 0, 'set_of_numbers'] = 999
df.loc[df['set_of_numbers'] == 5, 'set_of_numbers'] = 555

print (df)
Comment

pandas if python

import pandas as pd

names = {'First_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['First_name'])

df['name_match'] = df['First_name'].apply(lambda x: 'Match' if x == 'Bill' else 'Mismatch')

print (df)
Comment

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', 'name_match'] = 'Match'  
df.loc[df['First_name'] != 'Bill', 'name_match'] = 'Mismatch'  
 
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 :: docker python 3.11 
Python :: Read multiple csv files into separate dataframes Python 
Python :: scikit decision tree regressor 
Python :: python order list of dictionaries by value 
Python :: python update function 
Python :: json.dump 
Python :: do while in python 
Python :: list input python 
Python :: python oops 
Python :: python bool() 
Python :: graph outlier detection 
Python :: sum in python 
Python :: round down number python 
Python :: python find if part of list is in list 
Python :: python check for exception 
Python :: How to perform heap sort? 
Python :: string functions 
Python :: Matching a pattern in python 
Python :: takes 2 positional arguments but 3 were given 
Python :: max of a list in python 
Python :: Simple example of python strip function 
Python :: round down py 
Python :: fizzbuzz program in python 
Python :: pandas difference between dates in hours 
Python :: axes_style seaborn 
Python :: check if a number is integer or decimal in python 
Python :: python dataframe find no of true 
Python :: print function python 
Python :: sphinx themes 
Python :: python 3.6 release date 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =