Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack nested lists from a pandas series into a new DataFrame

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:

like image 346
Candice Avatar asked Dec 13 '22 13:12

Candice


2 Answers

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
like image 119
cs95 Avatar answered Dec 22 '22 16:12

cs95


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
like image 23
BENY Avatar answered Dec 22 '22 16:12

BENY