Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a dataframe using pandas?

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.

like image 572
Nikhil Mangire Avatar asked Dec 13 '22 15:12

Nikhil Mangire


1 Answers

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
like image 151
jezrael Avatar answered Dec 25 '22 07:12

jezrael