Chrissie M.
Chrissie M.

Reputation: 121

How to save in a new column part of an existing string

In a dataframe I have a column which contains a list of emails. My manager wants me to keep the name after the @ and before the . to a new column. I tried the following:

DF['newcolumn'] = DF['email'].split("@")[2].split(".")[0]

but it did not work. Any ideas?

Upvotes: 1

Views: 29

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Use the following with regex as delimiter:

df['email'].str.split('@|\.').str[-2]

MVCE:

df = pd.DataFrame({'email':['[email protected]',
                            '[email protected]',
                            '[email protected]']})

df['email'].str.split('@|\.').str[-2]

Output:

0         abc
1       candy
2    questinc
Name: email, dtype: object

Upvotes: 1

Related Questions