如何使用 pandas 将多行字符串合并为一个? [英] How to combine multiple rows of strings into one using pandas?

查看:62
本文介绍了如何使用 pandas 将多行字符串合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多行的DataFrame.有什么方法可以将它们组合成一个字符串?

I have a DataFrame with multiple rows. Is there any way in which they can be combined to form one string?

例如:

     words
0    I, will, hereby
1    am, gonna
2    going, far
3    to
4    do
5    this

预期输出:

I, will, hereby, am, gonna, going, far, to, do, this

推荐答案

您可以使用str.cat将每一行中的字符串连接起来.对于系列或列s,请输入:

You can use str.cat to join the strings in each row. For a Series or column s, write:

>>> s.str.cat(sep=', ')
'I, will, hereby, am, gonna, going, far, to, do, this'

这篇关于如何使用 pandas 将多行字符串合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆