I have a dataframe df and the column["shares"] of it seems to be a list within a list
0     [[10], [9], [9]]
1     [[3], [3], [2]]
2     [[17], [17], [18]]
How do I split this column into 3 columns :
col1 col2 col3
10   9     9
3    3     2
17   17    18
I tried df["shares"].apply(literal_eval)
But it gives me error malformed node or string: 
You will need to unpack your lists and re-construct your frame.
cols = ['col1', 'col2', 'col3']
pd.DataFrame(([c[0] for c in r] for r in df.shares.tolist()), columns=cols)
   col1  col2  col3
0    10     9     9
1     3     3     2
2    17    17    18
To generalise to lists with more than 3 sub-lists, you can use
pd.DataFrame(
    [c[0] for c in r] for r in df.shares.tolist()
).rename(columns=lambda x: f'col{x+1}')
   col1  col2  col3
0    10     9     9
1     3     3     2
2    17    17    18
                        Flatten the list of list , then using DataFrame constructor 
import itertools
pd.DataFrame(list(map(lambda x : list(itertools.chain(*x)),df.shares.tolist())))
    0   1   2
0  10   9   9
1   3   3   2
2  17  17  18
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With