I am a bit stuck, I have a working function that can be utilised using .apply(), however, I cannot seem to get it to work with .assign(). I'd like this to work with assign, so I can chain a number of transformations together.
Could anyone point me in the right direction to resolving the issue?
This works
data = {'heading': ['some men', 'some men', 'some women']}
dataframe = pd.DataFrame(data=data)
def add_gender(x):
if re.search("(womens?)", x.heading, re.IGNORECASE):
return 'women'
elif re.search("(mens?)", x.heading, re.IGNORECASE):
return 'men'
else:
return 'unisex'
dataframe['g'] = dataframe.apply(lambda ref: add_gender(ref), axis=1)
This does not work
dataframe = dataframe.assign(gender = lambda ref: add_gender(ref))
TypeError: expected string or bytes-like object
Is this because .assign() does not provide an axis argument? So perhaps the function is not looking for the right thing?
Having read the documentation .assign states you can generate a new column, so I assumed the output would be the same as .apply(axis=1)