-1
$\begingroup$

Column enter image description here

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.

$\endgroup$
1
  • $\begingroup$ 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 ? $\endgroup$ Jul 3, 2019 at 11:11

3 Answers 3

0
$\begingroup$

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))
$\endgroup$
0
$\begingroup$

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)
$\endgroup$
4
  • $\begingroup$ thank you for answering! However, all-code answers are discouraged; would you be able to add some corresponding text that provides explanation? $\endgroup$
    – Benji
    Jul 27, 2020 at 21:49
  • 1
    $\begingroup$ I added some explanations $\endgroup$
    – nimar
    Jul 28, 2020 at 0:23
  • $\begingroup$ Is it clear for you? $\endgroup$
    – nimar
    Jul 28, 2020 at 17:38
  • $\begingroup$ 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. $\endgroup$
    – Benji
    Jul 28, 2020 at 20:14
0
$\begingroup$

You could try this if this is simple and more readable:

Sample_data['Extracted_New_CI'] = Sample_data[Sample_data['CI Name'].isin(list_CI)]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.