Split (explode) pandas dataframe string entry to separate rows

后端 未结 22 3597
一向
一向 2020-11-21 05:03

I have a pandas dataframe in which one column of text strings contains comma-separated values. I want to split each CSV field and create a new row per entry (as

22条回答
  •  自闭症患者
    2020-11-21 05:23

    The string function split can take an option boolean argument 'expand'.

    Here is a solution using this argument:

    (a.var1
      .str.split(",",expand=True)
      .set_index(a.var2)
      .stack()
      .reset_index(level=1, drop=True)
      .reset_index()
      .rename(columns={0:"var1"}))
    

提交回复
热议问题