Split (explode) pandas dataframe string entry to separate rows

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

    How about something like this:

    In [55]: pd.concat([Series(row['var2'], row['var1'].split(','))              
                        for _, row in a.iterrows()]).reset_index()
    Out[55]: 
      index  0
    0     a  1
    1     b  1
    2     c  1
    3     d  2
    4     e  2
    5     f  2
    

    Then you just have to rename the columns

提交回复
热议问题