Split column into separate columns based on separator strings

后端 未结 2 2008
野性不改
野性不改 2021-01-28 19:40

For example we have a csv file with

name age address john 25 koramangala banglore #@ sales maneger

2条回答
  •  醉话见心
    2021-01-28 20:01

    Passing regular expression in the sep of read_csv

    import io
    t = """name ,age , address
    john,25,koramangala banglore +ACMAQA- sales maneger +ACUAJA- india
    harshuth rao ,36,belandur banglore +ACMAQA-  maneger +ACUAJA- india 
    vijay kumar,45,ulsoor banglore +ACMAQA- sales maneger +ACUAJA- india
    suhas,25,koramangala banglore +ACMAQA-analist +ACUAJA- india
    mithun,22,venkatapura banglore +ACMAQA- execitive +ACUAJA- india"""
    
    df = pd.read_csv(io.StringIO(t), 
                     sep='\s*\+ACMAQA-\s*|\s*\+ACUAJA-\s*|\s*,\s*', engine='python')
    df = df.reset_index()
    df.columns = ["name", "age", "city", "position", "country"]
    
    
        name          age                   city    position      country
    0   john           25   koramangala banglore    sales maneger   india
    1   harshuth rao   36   belandur banglore       maneger         india
    2   vijay kumar    45   ulsoor banglore sales   maneger         india
    3   suhas          25   koramangala banglore    analist         india
    4   mithun         22   venkatapura banglore    execitive       india
    

提交回复
热议问题