python - If value contains string, then set another column value -
i have dataframe in pandas column called 'campaign' has values this:
"uk-sample-car rental-car-broad-matchpost"
i need able pull out string contains word 'car rental' , set product column 'car'. hyphen not separating out word car, finding string way isn't possible.
how can achieve in pandas/python?
pandas sweet string functions can use
for example, this:
df['vehicle'] = df.campaign.str.extract('(car).rental').str.upper()
this sets column vehicle contained inside parenthesis of regular expression given extract function
.
also str.upper
makes uppercase
extra bonus:
if want assign vehicle not in original string, have take few more steps, still use string functions time str.contains
.
is_motorcycle = df.campaign.str.contains('motorcycle') df['vehicle'] = pd.series(["mc"] * len(df)) * is_motorcycle
the second line here creates series of "mc" strings, masks on entries found motorcycles.
if want combine multiple, suggest use map function:
vehicle_list = df.campaign.str.extract('(car).rental|(motorcycle)|(hotel)|(.*)') vehicle = vehicle_list.apply(lambda x: x[x.last_valid_index()], axis=1) df['vehicle'] = vehicle.map({'car':'car campaign', 'hotel':'hotel campaign'})
this first extracts data list of options per line. cases split | , last 1 catch-all needed series.apply function
below. series.map function
pretty straight forward, if captured data 'car', set 'car campaign', , 'hotel' set 'hotel campaign' etc.
Comments
Post a Comment