-2

I have a column named 'email' and I want to create a new column named 'company' that will take strings after the '@' up until the first '.' .

finaldf['email'].head(3)

0     person@company.com.br
1    woman@company.com.br
2    people@company.fr

I was able to figure out a way of doing it but it would take a couple of steps (lines of code)

1 Answers1

0

use extract to capture the string b/w @ and period.

df['email'].str.extract(r'@(.*?)\.' )
          0
0   company
1   company
2   company
Naveed
  • 11,495
  • 2
  • 14
  • 21
  • thank you. It is giving me the string including the dot (.) anyway to not include it? – Daniel Rondon Sep 23 '22 at 20:23
  • did you copy/paste the code or typed it? sounds like **\.** are within the parenthesis. It should be outside of the parenthesis after ?. the inner parenthesis are the capture group – Naveed Sep 23 '22 at 20:24