pandas 删除python Dataframe 列项目中的引号和括号

k7fdbhmy  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(324)

DF1

Place                             Actor
['new york','washington']         ['chris evans','John']    
['new york','los angeles']        ['kate','lopez']

我想删除每个列项目中的括号和引号:
预期产出:
DF1

Place                 Actor
new york,washington        chris evans,John   
new york,los angeles       kate,lopez

我的尝试:

cols = [Place,Actor]
df1[cols].apply(lambda x : x [' + ', '.join(df1[cols]) + ']')
wlp8pajw

wlp8pajw1#

如果有列表,请使用Series.str.join

cols = ['Place','Actor']
df1[cols] = df1[cols].apply(lambda x : x.str.join(', '))

编辑:如果有字符串,请使用Series.str.stripSeries.str.replace

cols = ['Place','Actor']
df1[cols] = df1[cols].apply(lambda x : x.str.strip('[]').str.replace("'","", regex=True))

相关问题