Python Forum

Full Version: splitting a Dataframe Column in two parts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'?
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
(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.