Yilmaz
Yilmaz

Reputation: 119

How to extract City From Purchase Address Column using Python

I want to create a new column in df called a city

This is how the column looks like in df data frame:

   **Purchase Address**
   917 1st St, Dallas, TX 75001
   682 Chestnut St, Boston, MA 0221

My Expected output should be a new column in df called a city where values between the comma (,) should be in place for example......

   **City**
   Dallas
   Boston

Upvotes: 0

Views: 1075

Answers (1)

Amit Kumar
Amit Kumar

Reputation: 633

below code will work if you don't have any NA values in the address column and the format of address is fixed.

df['city']=[x.split(',')[1] for x in df['Purchase Address'].values]

Upvotes: 1

Related Questions