Python Forum
splitting a Dataframe Column in two parts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: splitting a Dataframe Column in two parts (/thread-39299.html)



splitting a Dataframe Column in two parts - nafshar - Jan-27-2023

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'?


RE: splitting a Dataframe Column in two parts - deanhystad - Jan-27-2023

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



RE: splitting a Dataframe Column in two parts - nafshar - Jan-30-2023

(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.