Removing inverted commas and brackets from python dataframe column items

Question:

df1

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

I want to remove brackets and inverted commas from each column items:

Expected output:

df1

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

My try:

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

||

Answers:

Use Series.str.join if there are lists:

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

EDIT: If there are strings use Series.str.strip and Series.str.replace:

cols = ['Place','Actor']
df1[cols] = df1[cols].apply(lambda x : x.str.strip('[]').str.replace("'","", regex=True))
Answered By: jezrael
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.