How do I find a list of substrings in a dataframe against an array and use the array values to create a new column? For example, I started off using str.contains and typing out the actual string value(see below).
import pandas as pd
import numpy as np
#Filepath directory
csv_report = filepath
#Creates dataframe of CSV report
csv_df = pd.read_csv(csv_report)
csv_df['animal'] = np.where(csv_df.item_name.str.contains('Condor'), "Condor",
np.where(csv_df.item_name.str.contains('Marmot'), "Marmot",
np.where(csv_df.item_name.str.contains('Bear'),"Bear",
np.where(csv_df.item_name.str.contains('Pika'),"Pika",
np.where(csv_df.item_name.str.contains('Rat'),"Rat",
np.where(csv_df.item_name.str.contains('Racoon'),"Racoon",
np.where(csv_df.item_name.str.contains('Opossum'),"Opossum")))))))
How would I go about achieving the above code if the string values are in an array instead? Sample below:
import pandas as pd
import numpy as np
#Filepath directory
csv_report = filepath
#Creates dataframe of CSV report
csv_df = pd.read_csv(csv_report)
animal_list = np.array(['Condor', 'Marmot','Bear','Pika','Rat','Racoon','Opossum'])
numpystring dtypes for its strings. Columns with strings are object dtype, with Python strings. Use string methods and pandas own string enhancements.