如何将 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/