简体   繁体   English

如何有效地将系列索引添加到Pandas系列列表数组的每个列表元素中?

[英]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. 我有一系列2X1列表的数组,我需要展平两次。 Itertools.chain() will do this efficiently, but I want to retain the series index information. Itertools.chain()会有效地做到这一点,但我想保留系列索引信息。

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). 我在系列中尝试了一个非常基本的双循环来访问每个元素,但事实证明这非常低效(我的数据集有~1MM列表)。 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] [OUT]

[[0, ['a', '1']], [0, ['2', '3']], [1, ['b', '4']], [1, ['1', '5']], [1, ['7', '9']], [2, ['c', '6']], [2, ['7', '8']]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM