Home > Net >  Removing inverted commas and brackets from python dataframe column items
Removing inverted commas and brackets from python dataframe column items

Time:12-23

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])   ']')
   

CodePudding user response:

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))
  • Related