I have a dataframe with a column “CCR” whose values are as such : “Aaaa;Bbbb;Cccc”, or “Bbbb;;Cccc”, or “Cccc;Bbbb;Aaaa” or just “Bbbb”. I would like to extract the last substring part (after the “;”) and put it into another column “LastCCR”. If there is only one value and therefore no ‘;’ then the value stays the same in the LastCCR column.
I would like to use a lambda function: I used the code line below to extract the FIRST value but I don’t know how to alter it so as to extract the LAST value of my string. Can anyone help ?
df[1stCCR] = df['CCR'].apply(lambda x:x[0:x.index(';') if ';' in x else None])
Advertisement
Answer
df[1stCCR] = df['CCR'].apply(lambda x: x.split(";")[-1])