Split (explode) pandas dataframe string entry to separate rows

后端 未结 22 3482
一向
一向 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:12

    One-liner using split(___, expand=True) and the level and name arguments to reset_index():

    >>> b = a.var1.str.split(',', expand=True).set_index(a.var2).stack().reset_index(level=0, name='var1')
    >>> b
       var2 var1
    0     1    a
    1     1    b
    2     1    c
    0     2    d
    1     2    e
    2     2    f
    

    If you need b to look exactly like in the question, you can additionally do:

    >>> b = b.reset_index(drop=True)[['var1', 'var2']]
    >>> b
      var1  var2
    0    a     1
    1    b     1
    2    c     1
    3    d     2
    4    e     2
    5    f     2
    

提交回复
热议问题