Anurag Jhalaria
Anurag Jhalaria

Reputation: 11

Removing double space and single space in data frame simultaneously

I have a column where the names are separated by Single space, double space(there can be more) and I want to split the names by Fist Name and Last Name

df = pd.DataFrame({'Name': ['Steve  Smith', 'Joe Nadal', 
                           'Roger   Federer'],{'Age':[32,34,36]})

df['Name'] = df['Name'].str.strip()


df[['First_Name', 'Last_Name']] = df['Name'].str.split(" ",expand = True,)

Upvotes: 1

Views: 107

Answers (2)

Chris Adams
Chris Adams

Reputation: 18647

Use \s+ as your split pattern. This is the regex pattern meaning "one or more whitespace characters".

Also, limit number of splits with n=1. This means the string will only be split once (The first occurance of whitespace from left to right) - restricting the output to 2 columns.

df[['First_Name', 'Last_Name']] = df.Name.str.split('\s+', expand=True, n=1)

[out]

              Name  Age First_Name Last_Name
0     Steve  Smith   32      Steve     Smith
1        Joe Nadal   34        Joe     Nadal
2  Roger   Federer   36      Roger   Federer

Upvotes: 1

iamklaus
iamklaus

Reputation: 3770

this should do it

 df[['First_Name', 'Last_Name']] =  df.Name.apply(lambda x: pd.Series(list((filter(None, x.split(' '))))))

Upvotes: 1

Related Questions