python - 将 Pandas 中的一列转换为一个长字符串 (Python 3)

标签 python string python-3.x pandas dataframe

如何将 pandas 列转换为一个长字符串?

例如,转换以下DF:

Keyword
James
Went
To
The
Market

读作

Keyword
James went to the market

有什么帮助吗?

最佳答案

您可以先使用.tolist 将列转换为列表,然后使用.join 方法将所有单独的单词连接在一起。

print(df)

  Keyword
0   James
1    Went
2      To
3     The
4  Market

' '.join(df['Keyword'].tolist())

# output: 'James Went To The Market'

# or to put them in a dataframe with 1 row 1 column
pd.DataFrame(' '.join(df['Keyword'].tolist()), columns=['Keyword'], index=[0])

                    Keyword
0  James Went To The Market

关于python - 将 Pandas 中的一列转换为一个长字符串 (Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31866304/

相关文章:

python - 分组和拆分以避免泄漏

python - 在 python scipy/numpy 中按行过滤矩阵元素

python - 如何解决将推文数据插入 Apache Cassandra 数据库时遇到的错误?

c++ - 比较两个字符串看它们是否旋转

string - 字符串匹配中的前缀与后缀 Trie

python - Pandas:在分组数据框中选择一天频率的倍数的日期?

python - 为什么是Python 3.6.1。 pyenv中不可用?

linux - 以相反顺序打印由点分隔的字符串

python - 在 matplotlib 中使用 subplot_kw 在子图中创建极坐标投影

python - 在封闭范围内避免分配错误之前引用的 Pythonic 方法是什么?