Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe column remove string before the first specific character

I have a dataframe df with an 'Address' column. I would like to remove the street address (which theoretically is the part before the first comma) and keep on the city level address.

df

Address
777 Brockton Avenue, Abington, MA 2351
30 Memorial Drive, Avon, MA 2322
250 Hartford Avenue, Bellingham, MA 2019
700 Oak Street, Brockton, MA 2301
66-4 Parkhurst Rd, Chelmsford, MA 1824

Desired df

Address
Abington, MA 2351
Avon, MA 2322
Bellingham, MA 2019
Brockton, MA 2301
Chelmsford, MA 1824

I tried this following code but it remove all strings before all comma. I would like to only remove string before the first comma in the column.

df['Address'] = df['Address'].str.split(',').str.get(-1)

Thanks in advance!

like image 283
Matthias Gallagher Avatar asked Oct 18 '25 22:10

Matthias Gallagher


1 Answers

Add parameter n=1 for split by first comma:

df['Address'] = df['Address'].str.split(',', n=1).str.get(-1)
print (df)
                Address
0     Abington, MA 2351
1         Avon, MA 2322
2   Bellingham, MA 2019
3     Brockton, MA 2301
4   Chelmsford, MA 1824
like image 140
jezrael Avatar answered Oct 21 '25 12:10

jezrael