我有一个pandas.DataFrame
:df1
如下。
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"
有人可以帮我这个吗?
答案 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();