I have a dataframe column containing the following:
Audience
searchretargeting
data-capture-320x50
purchase-behavior-320x500
data-capture-728x90
I want to create a new column (Audience2) by splitting out the 'Audience' column based on the '-' delimiter, ideally would like to only keep the 1st element of the split ('data' not the 'capture-320x50')
If there is no '-' present I would like the new column to be populated with what was in 'Audience'(e.g. searchretargeting):
Audience Audience2
siteretargeting siteretargeting
data-capture-320x50 data
I know how to str split the Audience column, but looking to add in some type of logic to circumvent the new column being NaN when there is no '-' present in the column
df['Audience2']=df['Audience'].str.split('-').str[1]
This splits the Audience column and only retains the first element but I've been struggling with various if-else and apply-lambda statements to figure out how to pull in data that doesn't have '-' without it being NaN