I have following dataframe to process, DF
Name City
Hat, Richards Paris
Adams New york
Tim, Mathews Sanfrancisco
chris, Moya De Las Vegas
kate, Moris Atlanta
Grisham HA Middleton
James, Tom, greval Rome
And my expected dataframe should be as following, DF
Name Last_name City
Hat Richards Paris
Adams New york
Tim Mathews Sanfrancisco
chris Moya De Las Vegas
kate Moris Atlanta
Grisham HA Middleton
James, Tom greval Rome
Splitting should be done on last ',' and if there is no ',' then entire other words or phrase should fall in column 'Last_name' and 'Name' column should remain vacant.
Use str.split
with radd
for add ,
, last str.lstrip
:
df[['first','last']] = df['Name'].radd(', ').str.rsplit(', ', n=1, expand=True)
df['first'] = df['first'].str.lstrip(', ')
print (df)
Name City first last
0 Hat, Richards Paris Hat Richards
1 Adams New york Adams
2 Tim, Mathews Sanfrancisco Tim Mathews
3 chris, Moya De Las Vegas chris Moya De
4 kate, Moris Atlanta kate Moris
5 Grisham HA Middleton Grisham HA
6 James, Tom, greval Rome James, Tom greval
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With