Answers for "if in pandas"

5

make a condition statement on column pandas

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Posted by: Guest on May-12-2020
6

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')
Posted by: Guest on January-21-2021
1

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)
Posted by: Guest on February-05-2021
2

if condition dataframe python

df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Posted by: Guest on May-20-2020
2

make a condition statement on column pandas

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
Posted by: Guest on May-10-2020
0

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)
Posted by: Guest on February-05-2021

Python Answers by Framework

Browse Popular Code Answers by Language