繁体   English   中英

如何在 Python 中连接 Pandas 系列的行

[英]How to concatenate rows of a Pandas series in Python

我有一个包含许多行的 Python pandas 系列,这些行包含一个单词列表,例如:

25     [estimated, million, people, lived, vulnerable...
176                                   [cent, vulnerable]
7      [create, sound, policy, frameworks, poor, vuln...
299    [create, sound, policy, frameworks, cent, vuln...
283    [missing, international, levels, based, estima...
                             ...                        
63     [create, sound, policy, frameworks, world, pop...
259             [build, world, population, still, lived]
193    [create, sound, policy, frameworks, every, sta...
284    [cent, situation, remains, particularly, alarm...
43     [based, less, cent, share, property, inheritan...
Name: clean_text, Length: 300, dtype: object

如何将所有行的单词连接到一个列表中? 我试过了:

nameofmyfile.str.cat(sep=', ')

但我得到一个错误:

TypeError:不能使用带有推断 dtype 'mixed' 值的.str.cat。

这是一个hacky方式。

# step 1: Convert to a list
our_list = df["series"].tolist()

# step 2: Make a new empty list and build it up
new_list = []
for words in our_list:
    new_list += words

@Alexis 给出的解决方案很好,但我总是反对使用循环并投票支持矢量化。 我创建了非常相似的系列,就像问题中给出的那样,即:

>>> a
foo    [hi, hello, hey]
bar     [I, me, myself]
dtype: object

现在使用 numpy 中的连接方法, foo, bar的列表将连接在一起形成一个元素数组:

>>> import numpy as np
>>> np.concatenate(a.values)
array(['hi', 'hello', 'hey', 'I', 'me', 'myself'], dtype='<U6')

Now I dont think there should be any problem with a numpy array returned, still if you want output as list you can use inbuilt list() method or numpy.ndarray's .tolist() method to get output as a list.

暂无
暂无

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

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