pandas new column if another column contains string code example
Example: python contains string new column
import pandas as pd
import numpy as np
df = pd.DataFrame({
'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'],
'Name': ['John', 'Doe', 'William', 'Spark', 'Mark'],
'Occupation': ['Chemist', 'Accountant', 'Statistician',
'Statistician', 'Programmer'],
'Date Of Join': ['2018-01-25', '2018-01-26', '2018-01-26', '2018-02-26',
'2018-03-16'],
'Age': [23, 24, 34, 29, 40]})
df['Department'] = pd.np.where(df.Occupation.str.contains("Chemist"), "Science",
pd.np.where(df.Occupation.str.contains("Statistician"), "Economics",
pd.np.where(df.Occupation.str.contains("Programmer"), "Computer", "General")))
print(df)