You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Pandas Series的每个值转为列表并合并为嵌套列表?

解决方法

你可以用以下几种简单方式实现需求:

方法1:使用apply+lambda

通过apply对Series中的每个元素做列表包装,再转成嵌套列表:

result = df['A'].apply(lambda x: [x]).tolist()

执行后result的结果为:

[['I like this'], ['I like that']]

方法2:列表推导式(更简洁直观)

直接遍历Series的每个元素,将其包裹为单元素列表:

result = [[x] for x in df['A']]

执行结果和上述方法一致,写法更简洁,处理常规规模数据时效率也不错。

方法3:利用numpy重塑数组(可选)

如果习惯用numpy操作,可先转成numpy数组再调整形状,最后转为列表:

import numpy as np
result = df['A'].to_numpy().reshape(-1, 1).tolist()

以上方法都能满足gensim的输入格式要求。

内容的提问来源于stack exchange,提问作者resssslll

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 12:01:45