0
                         image_CONTAINS_OBJECT
       0      ['label', 'document', 'text', 'paper', 'poster...
       1      ['jaw', 'poster', 'crowd', 'word', 'people', '...
       2      ['clothing', 'adventure', 'building', 'apparel...
       3      ['reception room', 'furniture', 'person', 'ove...
       4      ['coat', 'people', 'apparel', 'court', 'interi...

This is the pandas dataframe I have. How do I convert it into a series such as the following? Thanks in advance.

       0 0 label
         1 document
         2 text
         3 paper
         4 poster
       1 0 jaw
         1 poster
         2 crowd
         3 word
         4 people
       2 0 clothing
         1 adventure
         2 building
         3 apparel
       ...
Neha Madhavan
  • 15
  • 1
  • 4
  • Does this answer your question? [How to unnest (explode) a column in a pandas DataFrame?](https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe) – sushanth Aug 17 '20 at 10:43
  • 1
    @sushanth - Partly dupe. – jezrael Aug 17 '20 at 10:45

1 Answers1

0

If there are lists in column and expected output is MultiIndex use:

out = df['image_CONTAINS_OBJECT'].apply(lambda x: pd.Series(x)).stack()
print (out)
0  0             label
   1          document
   2              text
   3             paper
   4            poster
1  0               jaw
   1            poster
   2             crowd
   3              word
   4            people
2  0          clothing
   1         adventure
   2          building
   3            appare
3  0    reception room
   1         furniture
   2            person
4  0              coat
   1            people
   2           apparel
   3             court
   4            interi
Name: image_CONTAINS_OBJECT, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252