917 1st St, Dallas, TX 75001
682 Chestnut St, Boston, MA 02215
669 Spruce St, Los Angeles, CA 90001
669 Spruce St, Los Angeles, CA 90001
so, i'm trying to extract the city and state from the given data...
def get_city_state(address):
asplit = address.split(",")
ssplit = address.split(" ")
city = asplit[1].split()[-1]
state = asplit[2].split()[0]
return city , state
all_data['City'] = all_data['Purchase Address'].apply(lambda x: f"{get_city_state(x)}")
all_data.head()
do you want this ?
all_data = pd.DataFrame({'Purchase Address': ['917 1st St, Dallas, TX 75001',
'682 Chestnut St, Boston, MA 02215',
'669 Spruce St, Los Angeles, CA 90001',
'669 Spruce St, Los Angeles, CA 90001']})
Just the city :
def get_city_state(address):
asplit = address.split(",")
ssplit = address.split(" ")
city = asplit[1].split()[-1]
state = asplit[2].split()[0]
return city
all_data['City'] = all_data['Purchase Address'].apply(get_city_state).to_list()
Just the States :
def get_city_state(address):
asplit = address.split(",")
ssplit = address.split(" ")
city = asplit[1].split()[-1]
state = asplit[2].split()[0]
return states
all_data['States'] = all_data['Purchase Address'].apply(get_city_state).to_list()
Both :
def get_city_state(address):
asplit = address.split(",")
ssplit = address.split(" ")
city = asplit[1].split()[-1]
state = asplit[2].split()[0]
return city , state
all_data[['City', 'State']] = all_data['Purchase Address'].apply(get_city_state).to_list()
Output :