Python 将单词列表(数据帧内)转换为一组单词

Python 将单词列表(数据帧内)转换为一组单词,python,pandas,dataframe,Python,Pandas,Dataframe,在我的dataframe中,我有一列数据作为列表,如[cell,protein,expression],我想将其转换为一组单词,如cell,protein,expression,它应该应用于dataframe的整个列。请建议可能的方法。问题在于df['Final_Text']不是一个列表,而是一个字符串。首先尝试使用ast.literal\u eval: import ast from io import StringIO # your sample df s = """ ,Final_Te

在我的dataframe中,我有一列数据作为列表,如[cell,protein,expression],我想将其转换为一组单词,如cell,protein,expression,它应该应用于dataframe的整个列。请建议可能的方法。

问题在于
df['Final_Text']
不是一个列表,而是一个字符串。首先尝试使用
ast.literal\u eval

import ast
from io import StringIO

# your sample df

s = """
,Final_Text
0,"['study', 'response', 'cell']"
1,"['cell', 'protein', 'effect']"
2,"['cell', 'patient', 'expression']"
3,"['patient', 'cell', 'study']"
4,"['study', 'cell', 'activity']"
"""

df = pd.read_csv(StringIO(s))

# convert you string of a list of to an actual list
df['Final_Text'] = df['Final_Text'].apply(ast.literal_eval)

# use a lambda expression with join to keep the text inside the list
df['Final_Text'] = df['Final_Text'].apply(lambda x: ', '.join(x))

    Unnamed: 0      Final_Text
0      0            study, response, cell
1      1            cell, protein, effect
2      2            cell, patient, expression
3      3            patient, cell, study
4      4            study, cell, activity
试试这个

data['column_name'] = data['column_name'].apply(lambda x: ', '.join(x))

您是否希望将数据帧中的列表转换为字符串?将列表
[细胞,蛋白质,表达式]
转换为字符串
细胞,蛋白质,表达式
?如果是这样的话,只需
df['col']=df['col'].apply(lambda x:','.join(x))
谢谢,但它的输出是c,e,l,l,,,p,r,o,t,e,i,n,,,e,x,p,r,e,s,s,i,o,n。如何得到一个完整的单词,如,细胞,蛋白质,表达式?你能展示一个样本数据框吗?我无法用dataframe
df=pd.dataframe({'col':[['cell','protein','expression']]})和doing
df['col']=df['col']].apply(lambda x:','join(x))
好的,很抱歉,请看一下第一页(Dataframe2),谢谢。成功了。如果数据格式为0,“[study,response,cell]不是(“0,['study','response','cell']”)。你能推荐代码df['Final_Text']=df['Final_Text']的替代方案吗?应用(ast.literal_eval)?