Answers for "python if column contains string then"

11

dataframe column contains string

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

column contains substring python

df2 = df.filter(regex='spike')
print(df2)
Posted by: Guest on January-08-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 "python if column contains string then"

Python Answers by Framework

Browse Popular Code Answers by Language