0

I have a CSV file that has a name and email column. My goal is to map a new column, which is to separate the column names into two columns, first names and last names. The following sample table.

Names, Email
aa bb, aa@bb.com
bb cc, bb@cc.com
cc dd, cc@dd.com

To be

First Name, Last Name, Email
aa, bb, aa@bb.com
bb, cc, bb@cc.com
cc, dd, cc@dd.com

honestly, I am currently only able to read files with Pandas, I have read a number of articles about mapping in Pandas, but I have not found the right one.

Thank you.

Pengguna
  • 4,636
  • 1
  • 27
  • 32
  • 2
    Did you check [str.split](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html) with expand equals true? – Dani Mesejo Feb 19 '19 at 03:14
  • 1
    Also check out [my answer](https://stackoverflow.com/a/54303523/4909087) to that question. – cs95 Feb 19 '19 at 04:32

1 Answers1

2

You could try:

df[['First_Name','Last_Name']]=df.pop('Names').str.split(" ",expand=True)
print(df)
        Email First_Name Last_Name
0   aa@bb.com         aa        bb
1   bb@cc.com         bb        cc
2   cc@dd.com         cc        dd
anky
  • 74,114
  • 11
  • 41
  • 70