Python:将文本字符串从DataFrame提取为长字符串

时间:2016-07-11 03:44:03

标签: python string pandas dataframe

我有一个pandas.DataFramedf1如下。

   date                  text                             name
     1      I like you hair, do you like it              screen1
     2      beautiful sun and wind                       screen2
     3      today is happy, I want to got school         screen3
     4      good movie                                   screen4
     5      thanks god                                   screen1

我想根据df1中的文本列值创建一个长文本字符串。预期结果如下所示:

    str_long = "I like you hair, do you like it beautiful sun and     
     wind today is happy, I want to got school good movie thanks god"

有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:2)

使用数据框列(.str.cat()对象)的Series方法:

df["text"].str.cat(sep=" ")

您也可以在数据框列上应用str.join()

" ".join(df["text"])

或者,您可以在Series实例上调用sum()(在这种情况下,您可能会丢失每个字符串之间的空格):

df["text"].sum()

答案 1 :(得分:1)

只需使用' '.join(df['text'].tolist())

即可
df = pd.DataFrame({'date': [1, 2, 3], 'text': ['I like your', 'beautiful sun', 'good movie']})

df
Out[68]: 
   date           text
0     1    I like your
1     2  beautiful sun
2     3     good movie

' '.join(df['text'].tolist())
Out[72]: 'I like your beautiful sun good movie'

<强>解释

Base b = new Derived();