Search code examples
pythonpandasdictionaryanalysis

Splitting multiple Dictionaries within a Pandas Column


I'm trying to split a dictionary with a list within a pandas column but it isn't working for me...

The column looks like so when called;

df.topics[3]

Output

"[{'urlkey': 'webdesign', 'name': 'Web Design', 'id': 659}, {'urlkey': 'productdesign', 'name': 'Product Design', 'id': 2993}, {'urlkey': 'internetpro', 'name': 'Internet Professionals', 'id': 10102}, {'urlkey': 'web', 'name': 'Web Technology', 'id': 10209}, {'urlkey': 'software-product-management', 'name': 'Software Product Management', 'id': 42278}, {'urlkey': 'new-product-development-software-tech', 'name': 'New Product Development: Software & Tech', 'id': 62946}, {'urlkey': 'product-management', 'name': 'Product Management', 'id': 93740}, {'urlkey': 'internet-startups', 'name': 'Internet Startups', 'id': 128595}]"

I want to only be left with the 'name' and 'id' to put into separate columns of topic_1, topic_2, and so forth.

Appreciate any help.


Solution

  • You can give this a try.

    import json
    df.topics.apply(lambda x :  {x['id']:x['name'] for x in json.loads(x.replace("'",'"'))} )
    

    Your output for the row you gave is :

    {659: 'Web Design',
     2993: 'Product Design',
     10102: 'Internet Professionals',
     10209: 'Web Technology',
     42278: 'Software Product Management',
     62946: 'New Product Development: Software & Tech',
     93740: 'Product Management',
     128595: 'Internet Startups'}