我有一个 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"
谁能帮我解决这个问题?
最佳答案
使用 .str.cat()
数据框列的方法(Series
对象):
df["text"].str.cat(sep=" ")
您也可以在数据框列上应用 str.join()
:
" ".join(df["text"])
或者,您可以直接调用 sum()
在 Series
实例上(虽然在这种情况下您可能会丢失每个单独字符串之间的空格):
df["text"].sum()
关于Python:将文本字符串从DataFrame提取为长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38298844/