如何将列表列转换为非嵌套列表? [英] How to convert list column to a non-nested list?
本文介绍了如何将列表列转换为非嵌套列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当列元素为列表时,如何将列转换为非嵌套列表?
How to convert a column to a non-nested list while the column elements are list?
例如,该列就像
column
[1, 2, 3]
[1, 2]
我想最后关注
[1,2,3,1,2]
但是现在有了column.tolist()
,我会得到
But now with column.tolist()
, I will get
[[1,2,3],[1,2]]
感谢您的帮助.我的目的是找到最简单(优雅)和最有效的方法来执行此操作.现在,我使用@jezrael方法.
Thanks for help. My intention is to find the most simple (elegant) and efficient method to do this. Now I use @jezrael method.
from itertools import chain
output = list(chain.from_iterable(df[column])
@piRSquared提供了最简单的方法,但速度可能较慢.
The simplest method is provided by @piRSquared, but maybe slower.
output = df[column].values.sum()
推荐答案
您可以使用 numpy.concatenate
:
You can use numpy.concatenate
:
print (np.concatenate(df['column'].values).tolist())
[1, 2, 3, 1, 2]
或者:
from itertools import chain
print (list(chain.from_iterable(df['column'])))
[1, 2, 3, 1, 2]
另一种解决方案,谢谢 juanpa .arrivillaga :
Another solution, thanks juanpa.arrivillaga:
print ([item for sublist in df['column'] for item in sublist])
[1, 2, 3, 1, 2]
时间:
df = pd.DataFrame({'column':[[1,2,3], [1,2]]})
df = pd.concat([df]*10000).reset_index(drop=True)
print (df)
In [77]: %timeit (np.concatenate(df['column'].values).tolist())
10 loops, best of 3: 22.7 ms per loop
In [78]: %timeit (list(chain.from_iterable(df['column'])))
1000 loops, best of 3: 1.44 ms per loop
In [79]: %timeit ([item for sublist in df['column'] for item in sublist])
100 loops, best of 3: 2.31 ms per loop
In [80]: %timeit df.column.sum()
1 loop, best of 3: 1.34 s per loop
这篇关于如何将列表列转换为非嵌套列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文