Akshay Mitra
Akshay Mitra

Reputation: 67

I have a an address field in the data frame which looks like this

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

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

Try using str.extract with a regex pattern:

df['address'] = df['address'].str.extract(r'\b(\D+)\b,')

Explanation

  • \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

Related Questions