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

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 :: Math Module ceil() Function in python 
Python :: iterating through a list in python 
Python :: count occurrences of character in string python using dictionary 
Python :: append write python 
Python :: count specific instances in a columb in pandas 
Python :: check if variable is function python 
Python :: how to learn python 
Python :: discordpy owner only command 
Python :: tryexept in python 
Python :: apyori 
Python :: how to calculate the variance of all columns in python 
Python :: crawl a folder python 
Python :: python string cut 
Python :: destroy label tkinter 
Python :: python help 
Python :: gurobi python example 
Python :: how to get first element of array in python 
Python :: a string starts with an uppercase python 
Python :: python dictionary multiple same keys 
Python :: python get column from grouped dataframe 
Python :: list get every 2nd element 
Python :: check file existence python 
Python :: python pandas how to get the dataframe size 
Python :: adding to python path 
Python :: pandas df represent a long column name with short name 
Python :: how to change character in string python 
Python :: how to open folder in python 
Python :: Python dir() built-in function 
Python :: how to get the last value in a list python 
Python :: python anonymous function 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =