I have:
df = pd.DataFrame({'col1': {0: 'success', 1: 'failed', 2: 'variable x 10', 3: 'variable xr 134', 4: 'error', 5: 'not found'}})
I want to create 3 columns depending on if a substring is present in the main column.
- if the column contains the word
variable, then I want to split the string and take the 2nd element - if the column contains the word
variable, then I want to split the string and take the 3rd element. this value can also be a string - if the column contains one of these key words,
success,fail,error, ornot found, then I want to place a key word ofSuccess,Fail, orError. - if none of the keywords are present, then I would like a
Noneor''to appear
I tried:
df['var']=np.where(df['col1'].str.contains('variable'), df['col1'].str.split(' '),'')
df['val']=np.where(df['col1'].str.contains('variable'), df['col1'].str.split(' '),'')
df['other']=np.where(df['col1'].str.contains('not found'|'error'),'Error',
np.where(df['col1'].str.contains('success'),'Success',
np.where(df['col1'].str.contains('fail'),'Fail','')))
but I get the error unsupported operand type(s) for |: 'str' and 'str' with the str.contains and cannot select specific portions of the split string, as I receive the error operands could not be broadcast together with shapes (6,) (3,) () when trying df['val']=np.where(df['col1'].str.contains('variable'), df['col1'].str.split(' ')[2],'')
Pictorially, this is what I would like to end up with

Any suggestions?
I would
extracteach of the three portions (if any), thenconcatthe whole thing :Output :