pythonpandasdataframetransform

How to transform dataframe that contains list in every row of each column


I have the following dataframe which is one of the output from for loop.

df = pd.DataFrame()

df['Score'] = [['0-0','1-1','2-2'],['0-0','1-1','2-2']]
df ['value'] =[[0.08,0.1,0.15],[0.07,0.12,0.06]]
df ['Team'] = ['A','B']

I want to transform each element of list of each row to each element of a column. The following is the expected output:

enter image description here

How can I transform it?


Solution

  • You can try of unstacking index, once after applying pd.Series on each list of dataframe

    df = pd.DataFrame()
    
    df['Score'] = [['0-0','1-1','2-2'],['0-0','1-1','2-2']]
    df ['value'] =[[0.08,0.1,0.15],[0.07,0.12,0.06]]    
    
    df.stack().apply(pd.Series).ffill(1).unstack(level=0).T.reset_index(drop=True)
    

    Out:

        Score   value   Team
    0   0-0     0.08    A
    1   0-0     0.07    B
    2   1-1     0.1     A
    3   1-1     0.12    B
    4   2-2     0.15    A
    5   2-2     0.06    B