Convert cells in dataframe with multiple values to multiple rows

前端 未结 3 1473
无人及你
无人及你 2021-01-14 15:17

My data is like this:

Name    test1     test2      Count
Emp1    X,Y        A           1
Emp2    X          A,B,C       2
Emp3    Z          C           3
<         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 15:41

    I am just fix your code , since I do not recommend the method you unnesting the dataframe , you can check the answer here, there are multiple nice way.

    df2 = df.test1.str.split(',').apply(pd.Series)
    df2.index = df.set_index(['Name', 'Count']).index
    df2=df2.stack().reset_index(['Name', 'Count'])
    df3 = df.test2.str.split(',').apply(pd.Series)
    df3.index = df.set_index(['Name', 'Count']).index
    df3=df3.stack().reset_index(['Name', 'Count'])
    

    Just do merge here

    df2.merge(df3,on=['Name', 'Count'],how='outer')
    Out[132]: 
       Name  Count 0_x 0_y
    0  Emp1      1   X   A
    1  Emp1      1   Y   A
    2  Emp2      2   X   A
    3  Emp2      2   X   B
    4  Emp2      2   X   C
    5  Emp3      3   Z   C
    

提交回复
热议问题