Python Pandas concatenate a Series of strings into one string

前端 未结 2 620
悲&欢浪女
悲&欢浪女 2020-12-02 02:04

In python pandas, there is a Series/dataframe column of str values to combine into one long string:

df = pd.DataFrame({\'text\' : pd.Series([\'Hello\', \'wor         


        
相关标签:
2条回答
  • 2020-12-02 02:29

    You can join a string on the series directly:

    In [3]:
    ' '.join(df['text'])
    
    Out[3]:
    'Hello world !'
    
    0 讨论(0)
  • 2020-12-02 02:29

    Apart from join, you could also use pandas string method .str.cat

    In [171]: df.text.str.cat(sep=' ')
    Out[171]: 'Hello world !'
    

    However, join() is much faster.

    0 讨论(0)
提交回复
热议问题