简体   繁体   中英

Removing inverted commas and brackets from python dataframe column items

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM