Reading array of text elements without quotes

Viewed 37

Have a pandas dataframe with records looking like below :

 0 [/computers_&_electronics,/computers_&_electronics/electronics_&_electrical,/computers_&_electronics/electronics_&_electrical/data_sheets_&_electronics_reference,/shopping,/shopping/consumer_resources,/shopping/consumer_resources/coupons_&_discount_offers]
 1 [/sports,/sports/college_sports,/sports/sporting_goods,/sports/sporting_goods/basketball_equipment,/sports/team_sports,/sports/team_sports/basketball]
 2 [/business_&_industrial,/business_&_industrial/advertising_&_marketing,/business_&_industrial/advertising_&_marketing/sales,/law_&_government,/law_&_government/legal,/law_&_government/legal/product_liability,/shopping,/shopping/consumer_resources]

I want to read each hierarchy (e.g : /sport/college) as an array element and then perform operations.But since there are no quotes present in the hierarchy ( ideally should have been '/sport/college', ... ) each record is being read as one big string.

I tried with literal_eval but didn't work.Any other pointers ? There are about 7 million records on which the array conversion needs to be performed so looking for a fast and scalable approach

1 Answers

You can remove [] and then split:

df['col'] = df['col'].str.strip('[]').str.split(',')
Related