Create Multiple New Columns Based on Pipe-Delimited Column in Pandas

前端 未结 1 1268
甜味超标
甜味超标 2021-01-27 05:08

I have a pandas dataframe with a pipe delimited column with an arbitrary number of elements, called Parts. The number of elements in these pipe-strings varies from 0 to over 10.

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 05:35

    You can use get_dummies and add_prefix:

    df.Parts.str.get_dummies().add_prefix('Part_')
    

    Output:

       Part_12  Part_34  Part_56
    0        1        1        1
    

    Edit for comment and counting duplicates.

    df = pd.DataFrame({'Parts':['12|34|56|12']}, index=[0])
    pd.get_dummies(df.Parts.str.split('|',expand=True).stack()).sum(level=0).add_prefix('Part_')
    

    Output:

       Part_12  Part_34  Part_56
    0        2        1        1
    

    0 讨论(0)
提交回复
热议问题