Pandas column of lists, create a row for each list element

前端 未结 10 863
有刺的猬
有刺的猬 2020-11-22 06:59

I have a dataframe where some cells contain lists of multiple values. Rather than storing multiple values in a cell, I\'d like to expand the dataframe so that each item in t

10条回答
  •  野性不改
    2020-11-22 07:34

    I found the easiest way was to:

    1. Convert the samples column into a DataFrame
    2. Joining with the original df
    3. Melting

    Shown here:

        df.samples.apply(lambda x: pd.Series(x)).join(df).\
    melt(['subject','trial_num'],[0,1,2],var_name='sample')
    
            subject  trial_num sample  value
        0         1          1      0  -0.24
        1         1          2      0   0.14
        2         1          3      0  -0.67
        3         2          1      0  -1.52
        4         2          2      0  -0.00
        5         2          3      0  -1.73
        6         1          1      1  -0.70
        7         1          2      1  -0.70
        8         1          3      1  -0.29
        9         2          1      1  -0.70
        10        2          2      1  -0.72
        11        2          3      1   1.30
        12        1          1      2  -0.55
        13        1          2      2   0.10
        14        1          3      2  -0.44
        15        2          1      2   0.13
        16        2          2      2  -1.44
        17        2          3      2   0.73
    

    It's worth noting that this may have only worked because each trial has the same number of samples (3). Something more clever may be necessary for trials of different sample sizes.

提交回复
热议问题