Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

make a condition statement on column pandas

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Comment

or condition in pandas

df1 = df[(df.a != -1) & (df.b != -1)]
Comment

or condition in pandas

df2 = df[(df.a != -1) | (df.b != -1)]
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') | (df['First_name'] == 'Emma'), 'name_match'] = 'Match'  
df.loc[(df['First_name'] != 'Bill') & (df['First_name'] != 'Emma'), 'name_match'] = 'Mismatch'  

print (df)
Comment

make a condition statement on column pandas

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
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 :: binary search python 
Python :: render() in django 
Python :: how to print all items in a list python 
Python :: continue statement python 
Python :: compare two datetime in python 
Python :: python pandas how to get the dataframe size 
Python :: driver find element with multiple classes python 
Python :: python node class 
Python :: python leetcode 
Python :: merge a list of dictionaries python 
Python :: square a number in python 
Python :: python set with counts 
Python :: while loop odd numbers python 
Python :: django cleanup settings 
Python :: get array dimension numpy 
Python :: importing python module from different directory 
Python :: compare times python 
Python :: python remove duplicates 
Python :: python urlparse get domain 
Python :: python community 
Python :: how to input n space separated integers in python 
Python :: 2d arrays using python numpy 
Python :: 2d array in python 
Python :: bubble sort with code optimization 
Python :: Filter Pandas rows by specific string elements 
Python :: how to rename files python 
Python :: ordenar lista python 
Python :: pydub play audio 
Python :: adding debugger in django code 
Python :: tkinter canvas text size 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =