Extracting Text Value if exists in another column or array

Viewed 52

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!

1 Answers

The regex pattern depends on which character you want to capture. By your desired output, you need to change the pattern as follows:

df['new_column'] = df['text'].str.extract(r"@([A-Za-z_]+([0-9]+)*)")[0]

Out[51]:
               text new_column
0            @bobby      bobby
1             @mike       mike
2         why @mike       mike
3        huh, @brad       brad
4             @brad       brad
5   cmon @Sunny_Bat  Sunny_Bat
6        @Sunny_Bat  Sunny_Bat
7          @But_ten    But_ten
8               @g2         g2
9         @Mikey/fj      Mikey
10         @4242343        NaN

Original:

You may try where with lambda

name_list = ['brad', 'mike']
df['text'].str.extract(r"@([A-Za-z]+)").where(lambda x: x[0].isin(name_list))

Out[1658]:
      0
0   NaN
1  mike
2  mike
3  brad
4  brad
5   NaN
6   NaN
7   NaN
Related