Learner
Learner

Reputation: 691

How to add comma in between strings inside square brackets in pandas

I have a pandas dataframe as:-

categories
['business $ finance'\n 'business $currency']
['entertainment $movies' 'entertainment &music']
['sports& volleyball' 'sports& football'\n 'sports$ baseball']
...............

All I want to convert these strings inside brackets into proper list format. Expected output as:-

categories
['business $ finance','business $currency']
['entertainment $movies','entertainment &music']
['sports&volleyball','sports& football','sports$ baseball']
....................

what I have tried so far:-

array_cat=[]
for i in df['categories']:
      s=i[0].split()
      array_cat.append(s)

Edit

df['categories'][0] is something like:-

"['business finance#mergers & acquisitions' 'business#industries#telecom'\n 'diseases#kidney' 'gaming #offline #console#xbox'\n 'gaming #offline #playstation' 'lifestyle#shopping' 'movies#genres#drama'\n 'music#genres#hiphop/rap' 'personal finance#tools' 'technology#trending'\n 'theme#historicalsites']"

Upvotes: 1

Views: 547

Answers (1)

jezrael
jezrael

Reputation: 862911

Use str.findall by all values between '':

df['categories'] = df['categories'].str.findall('\'(.*?)\'')
print (df)
                                          categories
0  [business finance#mergers & acquisitions, busi...
1        [entertainment$movies, entertainment&music]
2  [sports&volleyball, sports&football, sports$ba...

Upvotes: 1

Related Questions