Have Pandas column containing lists, how to pivot unique list elements to columns?

前端 未结 5 779
旧时难觅i
旧时难觅i 2021-02-07 22:39

I wrote a web scraper to pull information from a table of products and build a dataframe. The data table has a Description column which contains a comma separated string of attr

5条回答
  •  时光说笑
    2021-02-07 23:18

    Use pd.get_dummies

    cols = ['PRODUCTS', 'DATE']
    pd.get_dummies(
        df.set_index(cols).DESCRIPTION \
          .str.split(',\s*', expand=True).stack()
    ).groupby(level=cols).sum().astype(int)
    

提交回复
热议问题