Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert(text):
- # don't convert if it is not string
- if not isinstance(text, str):
- return text
- # don't convert if it is not string
- if 'S[' not in text:
- return text
- text = text.split('S[')[1] # split on S[
- #print('string:', text)
- text = text[:-1] # remove ] at the end
- #print('string:', text)
- parts = text.split(',') # split on ,
- #print('list:', parts)
- parts = [x.split('}')[1] for x in parts] # split on } to remove {...}
- #print('list:', parts)
- return parts
- # -------------------
- data = '''Product code,Options
- COWZGH,"{272}Size: S[{854}Newborn (2.5-6kg),{855}Big Baby (6-9kg)]"
- PHBC,
- ORGWBT0,"{14}Size: S[{49}Newborn (2.5-6kg),{50}Big Baby (6-9kg),{51}Preemie (1.5-2.5kg)]"
- COVWDBC,"{270}Size: S[{850}Newborn (2.5-6kg),{851}Big Baby (6-9kg)]"
- COVWMV,"{271}Size: S[{852}Newborn (2.5-6kg),{853}Big Baby (6-9kg)]"
- BZ-168,
- ORWNG,"{200}Size: S[{662}Newborn (2.5-6kg),{663}Big Baby (6-9kg)]"
- ORWNB13,"{199}Size: S[{660}Newborn (2.5-6kg),{661}Big Baby (6-9kg)]"
- HBCDTG,'''
- import io
- import pandas as pd
- file_like_object = io.StringIO(data)
- df = pd.read_csv(file_like_object)
- print('\n--- before ---\n')
- print(df)
- df['Options'] = df['Options'].apply(convert)
- print('\n--- after ---\n')
- print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement