user12809368
user12809368

Reputation:

Extract words in pandas dataframe's column

How can I select part of string in a data frame's column satisfying the following conditions?

Example:

Column

https://www.test.com
https://train.co.uk

In the first case I should extract the word after the first full stop, i.e. test; in the second case, I should consider the first word after //, i.e. train

Upvotes: 0

Views: 2111

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150825

Another option is to use regex with non-caption group:

df.Column.str.extract('//(?:www\.)?([^\.]*)')

Output:

       0
0   test
1  train

Upvotes: 1

BENY
BENY

Reputation: 323396

This is try to get the domain

import pandas as pd
import tldextract


df['domain'] = df.Column.map(lambda x : tldextract.extract(x).domain)

Upvotes: 1

Related Questions