Python Forum
splitting a Dataframe Column in two parts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting a Dataframe Column in two parts
#1
I have a dataframe column for emal addresses and I am trying to find the count of the top domains.
I am sppliting the email using the '@" as the separator and trying to grab the domain part as follows:

df['Email'].str.split('@')[1]

# I get the following list instead of the second part of the list:
[output]

['anthony41', 'reed.com']

[/output]
how can I just get the domain part? in this case 'reed.com'?
Reply
#2
import pandas as pd

df = pd.DataFrame({"Email": ["[email protected]", "[email protected]", "[email protected]"]})
df["domain"] = df["Email"].apply(lambda a: a.split("@")[1])
df["name"] = df["Email"].map(lambda a: a.split("@")[0])
print(df)
Output:
Email domain name 0 [email protected] gmail.com A 1 [email protected] outlook.com B 2 [email protected] aol.com C
Or
import pandas as pd

df = pd.DataFrame({"Email": ["[email protected]", "[email protected]", "[email protected]"]})
df[["Name", "Domain"]] = df["Email"].str.split("@", expand=True)
print(df)
Output:
Email Name Domain 0 [email protected] A gmail.com 1 [email protected] B outlook.com 2 [email protected] C aol.com
Reply
#3
(Jan-27-2023, 09:25 PM)deanhystad Wrote:
import pandas as pd

df = pd.DataFrame({"Email": ["[email protected]", "[email protected]", "[email protected]"]})
df["domain"] = df["Email"].apply(lambda a: a.split("@")[1])
df["name"] = df["Email"].map(lambda a: a.split("@")[0])
print(df)
Output:
Email domain name 0 [email protected] gmail.com A 1 [email protected] outlook.com B 2 [email protected] aol.com C
Or
import pandas as pd

df = pd.DataFrame({"Email": ["[email protected]", "[email protected]", "[email protected]"]})
df[["Name", "Domain"]] = df["Email"].str.split("@", expand=True)
print(df)
Output:
Email Name Domain 0 [email protected] A gmail.com 1 [email protected] B outlook.com 2 [email protected] C aol.com

deanhystad - Thank kindly for the help. - Both versions worked very well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 241 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 686 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 619 Feb-10-2023, 08:41 AM
Last Post: Scott
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 797 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,459 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 10,983 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,018 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Extract parts of a log-file and put it in a dataframe hasiro 4 6,122 Apr-08-2022, 01:18 PM
Last Post: hasiro
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,386 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Filter dataframe by datetime.date column glidecode 2 4,912 Dec-05-2021, 12:51 AM
Last Post: glidecode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020