0

I have the column having List of Dictionaries , the length of list is varying . Just like shown below:

 id             Categories
    
    1      [  { "S" : "Restaurants" },  { "S" : "Drinks" },  { "S" : "Catch Up With Friends" }]
    2      [  { "S" : "Drinks" },  { "S" : "Rooftop" },  { "S" : "Outside" }]
    3      [  { "S" : "Drinks" },  { "S" : "Rooftop" }]
    4      [  { "S" : "Drinks" },  { "S" : "Nights" }, {"S":"Restaurant"}]
    .
    .
    .

I want to place the list of dictionary into separate columns of DataFrame. Output should look like :

 Category 1     Category 2    Category 3  ....

Category 1 will represents (S:Restaurants)
Category 2 will represents (S:Drinks)
Category 3 will represents (S:Nights) etc ..

How is it possible , if anyone suggest better solution than this , it will also be appreciate able.My aim is just to unpack the list of dictionaries and place as a column into dataframe. If it is in integer form , it will be good.

Hamza
  • 530
  • 5
  • 27

2 Answers2

1

Try this:

df = pd.DataFrame({"categories":[[  { "S" : "Restaurants" },  { "S" : "Drinks" },  { "S" : "Catch Up With Friends" }],
                   [  { "S" : "Drinks" },  { "S" : "Rooftop" },  { "S" : "Outside" }],
                   [  { "S" : "Drinks" },  { "S" : "Rooftop" }],
                   [  { "S" : "Drinks" },  { "S" : "Nights" }, {"S":"Restaurant"}]]})

df = pd.DataFrame([i for i in df.categories])
print(df)

Output :

enter image description here

Rishin Rahim
  • 655
  • 4
  • 14
0

For me this looks like task for pandas.DataFrame.apply's expand mode, simple example:

import pandas as pd
df = pd.DataFrame({'x':[[{'A':1},{'B':2},{'C':3}],[{'D':4},{'E':5},{'F':6}]]})
df.apply(lambda x:x, axis=1, result_type='expand')
df = df.apply(lambda x:x[0], axis=1, result_type='expand')
print(df)

output

          0         1         2
0  {'A': 1}  {'B': 2}  {'C': 3}
1  {'D': 4}  {'E': 5}  {'F': 6}

This solution assumes that your original pandas.DataFrame has exactly 1 column.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • How to do if I have several columns , because there exists multiple columns also. I – Hamza Apr 29 '21 at 08:21
  • @Anwar create single column `pandas.DataFrame` from your `pandas.DataFrame`, proceed with it as described in answer, [`pandas.DataFrame.join`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.join.html) result to your original `pandas.DataFrame` in index-index mode. – Daweo Apr 29 '21 at 08:26
  • If you could edit the post by writing the code , it will be beneficial for me – Hamza Apr 29 '21 at 08:34