Splitting a column value into 2 new columns - Python Pandas

I have a dataframe that has a "name" column. With values ​​like James Cameron. I would like to split it into 2 new columns "First_Name" and "Last_Name", but there is no delimiter in the data, so I'm not really sure how. I understand that James is at position [0] and Cameron is at position [1], but I'm not sure if you can recognize this without a separator

df = pd.DataFrame({'name':['James Cameron','Martin Sheen'],
               'Id':[1,2]})
df

      

EDIT:

Vaishali's answer below worked fine, for the data I provided. I created this dataframe as an example. My real code looks like this

data[['First_Name','Last_Name']] = data.director_name.str.split(' ', expand = True)

      

and this unfortunately gives an error:

'Columns must be same length as key'

      

The column contains the same values ​​as my example. Any suggestions?

thank

+3


source to share


3 answers


You can split by space

df[['Name', 'Lastname']] = df.name.str.split(' ', expand = True)

    Id  name            Name    Lastname
0   1   James Cameron   James   Cameron
1   2   Martin Sheen    Martin  Sheen

      



EDIT: Handling the "Columns must be the same length as the key" error. Data can have some names with more than one space, for example: George Martin Jr. In this case, one way is to split on a space and use the first and second lines, ignoring the third if it exists

df['First_Name'] = df.name.str.split(' ', expand = True)[0]
df['Last_Name'] = df.name.str.split(' ', expand = True)[1]

      

+8


source


A slightly different way to do it:



df[['first_name', 'last_name']] = df.apply(lambda row: row['name'].split(), axis=1)

df
   Id           name first_name last_name
0   1  James Cameron      James   Cameron
1   2   Martin Sheen     Martin     Sheen

      

+1


source


I like this technique ... Not as fast as simple splitting, but it falls very conveniently in column names.

df.join(df.name.str.extract('(?P<First>\S+)\s+(?P<Last>\S+)', expand=True))

   Id           name   First     Last
0   1  James Cameron   James  Cameron
1   2   Martin Sheen  Martin    Sheen

      

+1


source







All Articles