How to split a column into three columns in pandas

后端 未结 3 1137
悲&欢浪女
悲&欢浪女 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:37

    Use list comprehension with dict comprehension for list of dictionaries and pass to DataFrame constructor:

    L = [{k:v for y in x.split('; ')  for k, v in dict([y.split(': ')]).items()} 
              for x in df.pop('Address')]
    
    df = df.join(pd.DataFrame(L, index=df.index))
    print (df)
       ID     Name    Country     State Sector
    0   1    Kohli      India     Delhi   SE25
    1   2   Sachin      India    Mumbai   SE39
    2   3  Ponting  Australia  Tasmania    NaN
    

    Or use split with reshape stack:

    df1 = (df.pop('Address')
             .str.split('; ', expand=True)
             .stack()
             .reset_index(level=1, drop=True)
             .str.split(': ', expand=True)
             .set_index(0, append=True)[1]
             .unstack()
             )
    print (df1)
    0    Country Sector     State
    0      India   SE25     Delhi
    1      India   SE39    Mumbai
    2  Australia    NaN  Tasmania
    
    df = df.join(df1)
    print (df)
       ID     Name    Country Sector     State
    0   1    Kohli      India   SE25     Delhi
    1   2   Sachin      India   SE39    Mumbai
    2   3  Ponting  Australia    NaN  Tasmania
    

提交回复
热议问题