How to create columns in pandas df with .apply and user defined function
I'm trying to create several columns in a pandas DataFrame at once, where each column name is a key in a dictionary and the function returns 1 if any of the values corresponding to that key are present.
My DataFrame has 3 columns, jp_ref, jp_title, and jp_description. Essentially, I'm searching the jp_descriptions for relevant words assigned to that key and populating the column assigned to that key with 1s and 0s based on if any of the values are found present in the jp_description.
I received the following error but don't understand it:
TypeError: ("'int' object is not callable", 'occurred at index jp_ref')
Your logic is wrong you are traversing list in a loop and after first iteration it return 0 or 1 so
jp_descriptionvalue is never compared with complete list.
You split the jp_description and check the common elements with technology dict if common elements exists it means substring is found so return 1 else 0
def term_search(doc,tech): doc = doc.split(" ") common_elem = list(set(doc).intersection(technologies[tech])) if len(common_elem)>0: return 1 return 0 for tech in technologies: data[tech] = data['jp_description'].apply(lambda x : term_search(x,tech))