I work on a dataset like this one and I want to explode it with the content of each list.
index col1 col2 col3
1 [A,B] [,xx] [1,2]
2 [A,C] [zz,xx] [3,4]
3 [D,] [zz,yy] [2,2]
However, the missing values in [,xx] and in [D,] generate errors and I am looking for a way to fill the empty position in these lists.
My idea is to fill the blank in the lists to get something like [NaN,xx] and [D,NaN], without specifying the index in the list because the blank is not always at the same position.
How can I achieve this please ?
[,xx](or[,'xx']) could not be a valid python object.Only
['xx']would be, but in this case there is no way to differentiate something that would be converted to['xx', NaN]or[NaN, 'xx'].Thus, assuming that you have strings representations of list-like objects (
'[,xx]'), you could strip the[/]and split on,before exploding:NB. in most recent versions of pandas, replace
applymapbymap.Output:
Used input: