加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何将带有字符串作为行的pandas数据转换为该字符串中的每个单词

发布时间:2020-12-20 12:13:54 所属栏目:Python 来源:网络整理
导读:如何将带有字符串作为行的pandas数据转换为该字符串中的每个单词作为同一列中的行? 例如: 0 I love python1 I hate programming 至 0 I1 love2 python 3 I4 hate5 programming 解决方法 加入空间系列并再次拆分: pd.Series(' '.join(s).split())0 I1 love
如何将带有字符串作为行的pandas数据转换为该字符串中的每个单词作为同一列中的行?

例如:

0 I love python
1 I hate programming

0 I
1 love
2 python 
3 I
4 hate
5 programming

解决方法

加入空间系列并再次拆分:

pd.Series(' '.join(s).split())

0              I
1           love
2         python
3              I
4           hate
5    programming
dtype: object

或者,您可以使用pd.Series.str.cat来完成连接

pd.Series(s.str.cat(sep=' ').split())

0              I
1           love
2         python
3              I
4           hate
5    programming
dtype: object

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读