python contains string new column

Solutions on MaxInterview for python contains string new column by the best coders in the world

showing results for - "python contains string new column"
Micaela
13 Jan 2020
1import pandas as pd
2import numpy as np
3 
4df = pd.DataFrame({
5    'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'],
6    'Name': ['John', 'Doe', 'William', 'Spark', 'Mark'],
7    'Occupation': ['Chemist', 'Accountant', 'Statistician',
8                   'Statistician', 'Programmer'],
9    'Date Of Join': ['2018-01-25', '2018-01-26', '2018-01-26', '2018-02-26',
10                     '2018-03-16'],
11    'Age': [23, 24, 34, 29, 40]})
12 
13df['Department'] = pd.np.where(df.Occupation.str.contains("Chemist"), "Science",
14                               pd.np.where(df.Occupation.str.contains("Statistician"), "Economics",
15                               pd.np.where(df.Occupation.str.contains("Programmer"), "Computer", "General")))
16 
17print(df)