I have a code that extracts the string in a column that has @ before the string, like below:
df['new_column'] = df[text].str.extract(r"@([A-Za-z]+)")
But what if sometimes the text column contains the string I want to extract that might be missing the @ sign in front of it? How can I also account for those so I dont miss them?
text
@bobby
@mike
why @mike
huh, @brad
@brad
cmon @Sunny_Bat
@Sunny_Bat
@But_ten
@g2
@Mikey/fj
@4242343
Also, can I make sure the extracted text exists in the another column of the df?
For example when I add isin to my code above, it outputs True or False, instead of that can I just output the actual text value itself?
df['new_column'] = df['text'].str.extract(r"@([A-Za-z]+)").isin(name_list)
where name_list is an array of all the unique values of the name_list column in the same df
This gives me a new column that says True or False when I would want the name or nan itself instead
Desired Output:
text | new column
@bobby bobby
@mike mike
why @mike mike
huh, @brad brad
@brad brad
cmon @Sunny_bat Sunny_bat
@Sunny_Bat Sunny_bat
@But_ten But_ten
@g2 g2
@Mikey/fj Mikey
@4242343 NaN
Thanks!