Applying A Function To Dataframe Column
I am trying to apply a function to a column of a dataframe and it keeps throwing an error. I need your help. The function is suppose to delete rows that do not contain none of the
Solution 1:
I think below function will solve ur purpose
def get_restuarant_business(data):
keywordz=['food','restuarant','bakery','deli','fast food','bars','coffee']
data=data.lower()
flag= False
ifdatain keywordz:
flag= True
return flag
call this
business_df['food_cat'] = business_df['categories'].apply(
get_restuarant_business)
filter where u have true
Solution 2:
Try this!
import numpy as np
business = pd.DataFrame({'categories':['tours, breweries, pizza, restaurants, food',
'chicken wings, burgers, caterers, street vend',
'breakfast & brunch, restaurants, french, sand',
'home & garden, nurseries & gardening, shopping']})
keywordz=['food','restaurants','bakery','deli','fast','food','bars','coffee']
rest_biz = business[business['categories'].apply(lambda x: np.any([Trueif w.lower() in keywordz elseFalsefor w in x.split(', ')]))]
# output
categories
0 tours, breweries, pizza, restaurants, food
Post a Comment for "Applying A Function To Dataframe Column"