I have a series of arrays of 2X1 lists that I need to flatten twice. Itertools.chain() will do this efficiently, but I want to retain the series index information.
I tried a very basic double loop through the series to access each element, but this has proved very inefficient (my dataset has ~1MM lists). Is there a more efficient way of achieving this?
Sample Series:
x = pd.Series([np.array([['a',1],[2,3]]), np.array([['b',4],[1,5],[7,9]]), np.array([['c',6],[7,8]])], name='x')
y=[]
for i in range(len(x)):
for c in x[i]:
y.append([i,c])
Input Series:
0 [[a, 1], [2, 3]]
1 [[b, 4], [1, 5], [7, 9]]
2 [[c, 6], [7, 8]]
Name: x, dtype: object
Expected Output:
[[0, [a, 1]]], [0, [2, 3]], [1, [b, 4]], [1, [1, 5]], [1, [7, 9]], [2, [c, 6]], [2, [7, 8]]]```
You could try with a list comprehension:
[[idx, v.tolist()] for idx, val in x.iteritems() for v in val]
[out]
[[0, ['a', '1']], [0, ['2', '3']], [1, ['b', '4']], [1, ['1', '5']], [1, ['7', '9']], [2, ['c', '6']], [2, ['7', '8']]]
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