[英]Python Pandas concatenate a Series of strings into one string
在python pandas中,有一个str值的Series / dataframe列要组合成一个长字符串:
df = pd.DataFrame({'text' : pd.Series(['Hello', 'world', '!'], index=['a', 'b', 'c'])})
目标:'Hello world!'
到目前为止,诸如df['text'].apply(lambda x: ' '.join(x))
方法只返回Series。
达到目标连接字符串的最佳方法是什么?
您可以直接在系列中join
字符串:
In [3]:
' '.join(df['text'])
Out[3]:
'Hello world !'
除了join
之外,您还可以使用pandas string方法.str.cat
In [171]: df.text.str.cat(sep=' ')
Out[171]: 'Hello world !'
但是, join()
要快得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.