python - 如何在 Python 中连接 Pandas 系列的行
问题描述
我有一个包含许多行的 Python 熊猫系列,这些行包含一个单词列表,例如:
25 [estimated, million, people, lived, vulnerable...
176 [cent, vulnerable]
7 [create, sound, policy, frameworks, poor, vuln...
299 [create, sound, policy, frameworks, cent, vuln...
283 [missing, international, levels, based, estima...
...
63 [create, sound, policy, frameworks, world, pop...
259 [build, world, population, still, lived]
193 [create, sound, policy, frameworks, every, sta...
284 [cent, situation, remains, particularly, alarm...
43 [based, less, cent, share, property, inheritan...
Name: clean_text, Length: 300, dtype: object
如何将所有行的单词连接到一个列表中?我试过了:
nameofmyfile.str.cat(sep=', ')
但我得到一个错误:
TypeError:不能将 .str.cat 与推断的 dtype 'mixed' 的值一起使用。
解决方案
这是一个hacky方式。
# step 1: Convert to a list
our_list = df["series"].tolist()
# step 2: Make a new empty list and build it up
new_list = []
for words in our_list:
new_list += words
推荐阅读
- java - NullPointerException - getCellTpeEnum() POI
- php - 当 2 个产品来自不同类别时,更改 woocommerce 购物车页面中的“数量”文本
- java - 如何为 JMX 和 Java 任务控制导入 lib?
- html - 使用 CSS 关键帧显示一个又一个内容
- python - 从 C 代码序列化数组数据,在 Python 中反序列化
- angular - 角度 6+。如何扩展模板添加新的动作,如 JSF?
- security - 从 PGP 签名中看出哪些算法在其创建过程中起作用
- python - Pandas DataFrame 中的交叉表
- d3.js - c3.js - 隐藏特定数据集的工具提示
- node.js - 错误:ffmpeg 以代码 1 退出:未找到选项帧速率