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

前端 未结 10 768
有刺的猬
有刺的猬 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:29

    Trying to work through Roman Pekar's solution step-by-step to understand it better, I came up with my own solution, which uses melt to avoid some of the confusing stacking and index resetting. I can't say that it's obviously a clearer solution though:

    items_as_cols = df.apply(lambda x: pd.Series(x['samples']), axis=1)
    # Keep original df index as a column so it's retained after melt
    items_as_cols['orig_index'] = items_as_cols.index
    
    melted_items = pd.melt(items_as_cols, id_vars='orig_index', 
                           var_name='sample_num', value_name='sample')
    melted_items.set_index('orig_index', inplace=True)
    
    df.merge(melted_items, left_index=True, right_index=True)
    

    Output (obviously we can drop the original samples column now):

                     samples  subject  trial_num sample_num  sample
    0    [1.84, 1.05, -0.66]        1          1          0    1.84
    0    [1.84, 1.05, -0.66]        1          1          1    1.05
    0    [1.84, 1.05, -0.66]        1          1          2   -0.66
    1    [-0.24, -0.9, 0.65]        1          2          0   -0.24
    1    [-0.24, -0.9, 0.65]        1          2          1   -0.90
    1    [-0.24, -0.9, 0.65]        1          2          2    0.65
    2    [1.15, -0.87, -1.1]        1          3          0    1.15
    2    [1.15, -0.87, -1.1]        1          3          1   -0.87
    2    [1.15, -0.87, -1.1]        1          3          2   -1.10
    3   [-0.8, -0.62, -0.68]        2          1          0   -0.80
    3   [-0.8, -0.62, -0.68]        2          1          1   -0.62
    3   [-0.8, -0.62, -0.68]        2          1          2   -0.68
    4    [0.91, -0.47, 1.43]        2          2          0    0.91
    4    [0.91, -0.47, 1.43]        2          2          1   -0.47
    4    [0.91, -0.47, 1.43]        2          2          2    1.43
    5  [-1.14, -0.24, -0.91]        2          3          0   -1.14
    5  [-1.14, -0.24, -0.91]        2          3          1   -0.24
    5  [-1.14, -0.24, -0.91]        2          3          2   -0.91
    

提交回复
热议问题