python - 如何将带有字符串作为行的 Pandas 数据转换为该字符串中的每个单词作为同一列中的行?

标签 python string pandas

如何将以字符串为行的 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

关于python - 如何将带有字符串作为行的 Pandas 数据转换为该字符串中的每个单词作为同一列中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48849632/

相关文章:

python - Redis 流水线发送 200 条指令,只有 189 个答案

java - 如何使用java中的两个循环迭代从二维列表中删除元素

php - 如果文本描述旁边包含图像,如何对齐?

python - `DataFrame` 行的内存高效过滤

python - 循环遍历 pandas xlsxwriter 表

python - FastAPI 重定向给出方法不允许的错误

python - 为什么 numpy 的 asinh 输出因操作系统而异?

vb.net - 如何通过 "String Name"获取控制属性?

c++ - 在 map::at() 中内存字符串表示差异抛出错误

python - 对 pandas 数据框进行哈希计算以进行计算列缓存