9

For example, if I have a home address like this:

71 Pilgrim Avenue, Chevy Chase, MD

in a column named 'address'. I would like to split it into columns 'street', 'city', 'state', respectively.

What is the best way to achieve this using Pandas ?

I have tried df[['street', 'city', 'state']] = df['address'].findall(r"myregex").

But the error I got is Must have equal len keys and value when setting with an iterable.

Thank you for your help :)

2 Answers 2

21

You can use split by regex ,\s+ (, and one or more whitespaces):

#borrowing sample from `Allen`
df[['street', 'city', 'state']] = df['address'].str.split(',\s+', expand=True)
print (df)
                              address id             street          city  \
0  71 Pilgrim Avenue, Chevy Chase, MD  a  71 Pilgrim Avenue   Chevy Chase   
1         72 Main St, Chevy Chase, MD  b         72 Main St   Chevy Chase   

  state  
0    MD  
1    MD  

And if need remove column address add drop:

df[['street', 'city', 'state']] = df['address'].str.split(',\s+', expand=True)
df = df.drop('address', axis=1)
print (df)
  id             street         city state
0  a  71 Pilgrim Avenue  Chevy Chase    MD
1  b         72 Main St  Chevy Chase    MD
Sign up to request clarification or add additional context in comments.

1 Comment

u know what, this split with expand param is terrific, wish I could upvote this one 100 times
3
df = pd.DataFrame({'address': {0: '71 Pilgrim Avenue, Chevy Chase, MD',
      1: '72 Main St, Chevy Chase, MD'},
     'id': {0: 'a', 1: 'b'}})
#if your address format is consistent, you can simply use a split function.
df2 = df.join(pd.DataFrame(df.address.str.split(',').tolist(),columns=['street', 'city', 'state']))
df2 = df2.applymap(lambda x: x.strip())

1 Comment

Thank you Allen ! Great code :) The reason I didn't use split because I would like to remove the whitespace in the string as well. Is there a way to achieve that from split ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.