How to split one column into multiple columns in Pandas using regular expression?

后端 未结 2 1652
鱼传尺愫
鱼传尺愫 2021-02-06 07:01

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 col

相关标签:
2条回答
  • 2021-02-06 07:52

    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
    
    0 讨论(0)
  • 2021-02-06 07:54
    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())
    
    0 讨论(0)
提交回复
热议问题