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!
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.