Skip to content
Advertisement

How to remove domain of a websites on pandas dataframe

Here’s the dataset

Id  Websites
1   facebook.com
2   linked.in
3   stackoverflow.com
4   harvard.edu
5   ugm.ac.id

Heres’s my expected output

Id  Name
1   facebook
2   linked
3   stackoverflow
4   harvard
5   ugm

Advertisement

Answer

You can use a regex to get the part before the first dot, combined with pop to remove the Website column:

df['Name'] = df.pop('Websites').str.extract('([^.]+)')

output:

   Id           Name
0   1       facebook
1   2         linked
2   3  stackoverflow
3   4        harvard
4   5            ugm
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement