Answers for "create a column in a dataframe python that check if a row contains a string"

11

dataframe column contains string

df[df['A'].str.contains("hello")]
Posted by: Guest on September-28-2020
0

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)
Posted by: Guest on December-14-2020

Code answers related to "create a column in a dataframe python that check if a row contains a string"

Python Answers by Framework

Browse Popular Code Answers by Language