Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add the series index to each list element a Pandas series of arrays of lists efficiently?

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]]]```
like image 396
Kshiteesh Dorle Avatar asked Dec 29 '25 18:12

Kshiteesh Dorle


1 Answers

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']]]
like image 160
Chris Adams Avatar answered Jan 02 '26 06:01

Chris Adams