1df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')
2
1import pandas as pd
2
3names = {'First_name': ['Jon','Bill','Maria','Emma']}
4df = pd.DataFrame(names,columns=['First_name'])
5
6df.loc[(df['First_name'] == 'Bill') | (df['First_name'] == 'Emma'), 'name_match'] = 'Match'
7df.loc[(df['First_name'] != 'Bill') & (df['First_name'] != 'Emma'), 'name_match'] = 'Mismatch'
8
9print (df)
10
1import pandas as pd
2
3numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
4df = pd.DataFrame(numbers,columns=['set_of_numbers'])
5
6df['equal_or_lower_than_4?'] = df['set_of_numbers'].apply(lambda x: 'True' if x <= 4 else 'False')
7
8print (df)
9
1import pandas as pd
2
3names = {'First_name': ['Jon','Bill','Maria','Emma']}
4df = pd.DataFrame(names,columns=['First_name'])
5
6df['name_match'] = df['First_name'].apply(lambda x: 'Match' if x == 'Bill' else 'Mismatch')
7
8print (df)
9
1import pandas as pd
2
3names = {'First_name': ['Jon','Bill','Maria','Emma']}
4df = pd.DataFrame(names,columns=['First_name'])
5
6df.loc[df['First_name'] == 'Bill', 'name_match'] = 'Match'
7df.loc[df['First_name'] != 'Bill', 'name_match'] = 'Mismatch'
8
9print (df)
10
1import pandas as pd
2
3numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10,0,0]}
4df = pd.DataFrame(numbers,columns=['set_of_numbers'])
5print (df)
6
7df.loc[df['set_of_numbers'] == 0, 'set_of_numbers'] = 999
8df.loc[df['set_of_numbers'] == 5, 'set_of_numbers'] = 555
9
10print (df)
11