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

标签 python string pandas dataframe

我有一个 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/

相关文章:

python - 计算列表中值的重复次数并生成输出文件

python - 如何在不影响安全性的情况下授予 celery 足够的权限来运行根文件?

python - 为什么我得不到并发执行?

string - 包括传递字符串:Powershell脚本中-Replace Variable的符号

android - 将可编辑内容转换为字符串

python - 仅对 pandas DataFrame 的一部分进行重新排序

python - Pyramid :路由模式和约束

javascript - 在javascript中的2个字节之间添加一个字符

python - pandas fillna 不适用于数据集的子集

python - 如何将多个数据帧列合并为一个给定的每列都有 nan 值