Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Flatten a list of list within a column?

Tags:

python

pandas

I am trying to flatten a column which is a list of lists:

    var         var2
0   9122532.0   [[458182615.0], [79834910.0]]
1   79834910.0  [[458182615.0], [9122532.0]]
2   458182615.0 [[79834910.0], [9122532.0]]

I want:

    var         var2
0   9122532.0   [458182615.0, 79834910.0]
1   79834910.0  [458182615.0, 9122532.0]
2   458182615.0 [79834910.0, 9122532.0]

Applying

sample8['var2'] = sample8['var2'].apply(chain.from_iterable).apply(list)

Gives me:

    var1        var2
0   9122532.0   [[, 4, 5, 8, 1, 8, 2, 6, 1, 5, ., 0, ], [, 7, ...
1   79834910.0  [[, 4, 5, 8, 1, 8, 2, 6, 1, 5, ., 0, ], [, 9, ...
2   458182615.0 [[, 7, 9, 8, 3, 4, 9, 1, 0, ., 0, ], [, 9, 1, ...
like image 916
spitfiredd Avatar asked Feb 04 '23 17:02

spitfiredd


1 Answers

Data:

In [162]: df
Out[162]:
           var                           var2
0    9122532.0  [[458182615.0], [79834910.0]]
1   79834910.0   [[458182615.0], [9122532.0]]
2  458182615.0    [[79834910.0], [9122532.0]]

Solution: use np.ravel():

In [163]: df['var2'] = df['var2'].apply(np.ravel)

In [164]: df
Out[164]:
           var                       var2
0    9122532.0  [458182615.0, 79834910.0]
1   79834910.0   [458182615.0, 9122532.0]
2  458182615.0    [79834910.0, 9122532.0]
like image 179
MaxU - stop WAR against UA Avatar answered Feb 07 '23 08:02

MaxU - stop WAR against UA