Applying Pandas melt() on a dataframe with multiple variable columns

后端 未结 2 1327
时光取名叫无心
时光取名叫无心 2021-01-27 09:56

I have a dataframe. Rows are unique persons and columns are various action types taken. I need the data restructured to show the individual events by row. Here is my current and

2条回答
  •  不要未来只要你来
    2021-01-27 10:32

    r = df.roles
    c = df.roles.str.count(',') + 1
    i = df.index
    df.loc[i.repeat(c)].assign(roles=','.join(r).split(','))
    
      company  employer_id                roles
    0       a            1             engineer
    0       a            1       data_scientist
    0       a            1            architect
    1       b            2             engineer
    1       b            2  front_end_developer
    

提交回复
热议问题