Reputation: 67
My Address column of the dataframe looks like this.
874 E 139th St Mott Haven, BX
455 E 148th St South Bronx, BX
3952 3rd Ave Tremont, BX
I want this column to look like this.
St Mott Haven
St South Bronx
Ave Tremont
how can I do that using pandas.
Upvotes: 2
Views: 79
Reputation: 18647
Try using str.extract
with a regex pattern:
df['address'] = df['address'].str.extract(r'\b(\D+)\b,')
\b
word boundary(\D+)\b,
capture group - any characters, including whitespace that does not contain a number, upto but not including a comma.Upvotes: 2