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, ...
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]
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