[英]How to concatenate rows of a Pandas series in Python
I have a Python pandas series containing many rows, and these rows contain a list of words, eg:我有一个包含许多行的 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
How can I concatenate all of the rows' words into a single list?如何将所有行的单词连接到一个列表中? I've tried:我试过了:
nameofmyfile.str.cat(sep=', ')
But I got an error:但我得到一个错误:
TypeError: Cannot use.str.cat with values of inferred dtype 'mixed'. TypeError:不能使用带有推断 dtype 'mixed' 值的.str.cat。
Here is a hacky way.这是一个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
The given solution is good by @Alexis, but I'm always against using loops and vote for vectorization. @Alexis 给出的解决方案很好,但我总是反对使用循环并投票支持矢量化。 I have created very similar Series just like given in question, which is:我创建了非常相似的系列,就像问题中给出的那样,即:
>>> a
foo [hi, hello, hey]
bar [I, me, myself]
dtype: object
Now using concatenate method from numpy, the lists of foo, bar
will be concatenated together to form a single array of elements:现在使用 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. 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.