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

如何将DataFrame列中的列表合并为单个列表并去除引号

解决方法

这里有两种简单高效的方式实现你的需求:

方法一:列表推导式

直接通过嵌套遍历text列的子列表,同时去除每个元素首尾的单引号:

import pandas as pd

df = pd.DataFrame({'title': ['issue regarding app', 'graphics should be better'], 'text': [["'app'", "'load'", "'slowly'"], ["'interface'", "'need'", "'to'", "'look'", "'nicer'"]]})

lst = [item.strip("'") for sublist in df['text'] for item in sublist]
print(lst)
# 输出:['app', 'load', 'slowly', 'interface', 'need', 'to', 'look', 'nicer']

方法二:Pandas内置方法

利用explode()将嵌套列表展开为单行元素,再通过str.strip()处理单引号,最后转为列表:

import pandas as pd

df = pd.DataFrame({'title': ['issue regarding app', 'graphics should be better'], 'text': [["'app'", "'load'", "'slowly'"], ["'interface'", "'need'", "'to'", "'look'", "'nicer'"]]})

lst = df['text'].explode().str.strip("'").tolist()
print(lst)
# 输出:['app', 'load', 'slowly', 'interface', 'need', 'to', 'look', 'nicer']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 04:25:16