# Comparing a column value to list of values and if it contains the value then assigning that list value to new Column

Column

and wanted to check with this list

list_CI = ['Application', 'Server & Storage', 'Active Directory', 'Please Select Value', 'SAP', 'WPS-Core Infra Services', 'Workplace', 'WPS-Telecoms', 'Citrix', 'Networks', 'WPS-Connectivity Support', 'WPS-Groupware', 'WPS-Smarthands', 'Networks & Telecomms']

I tried to apply lambda but fail. How can solve through regex or any other method?

Please hint and shed some light. Thanks

• Can you clarify your question: what do you mean by if it contains this value ? do you mean is the string in your list a substring of the string in the column ? – Robin Nicole Jul 3 '19 at 11:11

This is the solution, after 4-5 attempt this code solves the problem

 def extractServiceName(x):
if x in  list_CI:
return x
else:
return ''

Sample_data['Extracted_New_CI'] = Sample_data['CI Name'].apply(lambda x : extractServiceName(x))


I would recommend using "apply" method in pandas, which allows you to apply a function along an axis of the DataFrame.

So, what I did is to write a function that checks whether a given row of data frame contains one of the values in the list or not. If it contains one of the values it returns that value; otherwise, it returns None.

Then, I applied this function along axis 1 of the data frame using "apply" method.

Also, I tried to use the lower case of each word in the list and each row to prevent any consequent issues; you can remove that if the capital or lower case matter in your case.

def check_value(x):
for i in list_CI:
if i.lower() in x.lower():
return i
return None

df['new_col'] = df.apply(lambda x['CI Name']: check_value(x), axis = 1)

• thank you for answering! However, all-code answers are discouraged; would you be able to add some corresponding text that provides explanation? – Benji Albert Jul 27 at 21:49
• I added some explanations – nimar Jul 28 at 0:23
• Is it clear for you? – nimar Jul 28 at 17:38
• Yes, thank you for adding the explanations! I was not having this particular problem, but for those who are having this problem, they would very much appreciate the text you added. – Benji Albert Jul 28 at 20:14