0

I greatly appreciate your effort in trying to work out the formula to delete bracket and apostrophe but so far it hasn't quite worked.

Data
['IMI', ‘50’]

Out put Should be like this
IMI,50

I used this method but not working

dataFrame['group_code'] = dataFrame['groups'].apply(lambda x: ', '.join([s[1:-1] for s in x]))
  • Does the column contain a string or a list? If it's a string, you're just removing the first and last chracters, then putting `,` between all the other characters, which isn't what you want. If it's a list, you shouldn't slice it, just use `','.join(s)` – Barmar Apr 17 '23 at 23:49

1 Answers1

0

If the column contains strings, you should use .str.replace() to remove the characters you don't want:

dataframe['groups'].str.replace(r"[\[\]']", "", regex=True)

If the column contains lists, don't slice it, since the brackets aren't list elements.

dataframe['groups'].apply(lambda l: ','.join(l))

See Pandas Column join list values

Barmar
  • 741,623
  • 53
  • 500
  • 612