How to split a column into three columns in pandas

后端 未结 3 1097
悲&欢浪女
悲&欢浪女 2021-01-21 17:26

I have a data frame as shown below

ID  Name     Address
1   Kohli    Country: India; State: Delhi; Sector: SE25
2   Sachin   Country: India; State: Mumbai; Secto         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-21 17:40

    You are almost there

    cols = ['ZONE', 'State', 'Sector']
    df[cols] = pd.DataFrame(df['ADDRESS'].str.split('; ',2).tolist(),
                                       columns = cols)
    
    for col in cols:
        df[col] = df[col].str.split(': ').apply(lambda x:x[1])
    

提交回复
热议问题