cyc*_*out 8 string string-concatenation series python-3.x pandas
在python pandas中,有一个str值的Series/dataframe列要组合成一个长字符串:
df = pd.DataFrame({'text' : pd.Series(['Hello', 'world', '!'], index=['a', 'b', 'c'])})
Run Code Online (Sandbox Code Playgroud)
目标:'Hello world!'
到目前为止,df['text'].apply(lambda x: ' '.join(x))
只有返回系列的方法.
达到目标连接字符串的最佳方法是什么?
EdC*_*ica 16
您可以join
直接在系列中使用字符串:
In [3]:
' '.join(df['text'])
Out[3]:
'Hello world !'
Run Code Online (Sandbox Code Playgroud)
除此之外join
,您还可以使用pandas字符串方法.str.cat
In [171]: df.text.str.cat(sep=' ')
Out[171]: 'Hello world !'
Run Code Online (Sandbox Code Playgroud)
但是,join()
速度要快得多。
归档时间: |
|
查看次数: |
8205 次 |
最近记录: |