pythonstringlistpandasseries

Pandas Series of lists to one series


I have a Pandas Series of lists of strings:

0                           [slim, waist, man]
1                                [slim, waistline]
2                                     [santa]

As you can see, the lists vary by length. I want an efficient way to collapse this into one series

0 slim
1 waist
2 man
3 slim
4 waistline
5 santa

I know I can break up the lists using

series_name.split(' ')

But I am having a hard time putting those strings back into one list.

Thanks!


Solution

  • Here's a simple method using only pandas functions:

    import pandas as pd
    
    s = pd.Series([
        ['slim', 'waist', 'man'],
        ['slim', 'waistline'],
        ['santa']])
    

    Then

    s.apply(pd.Series).stack().reset_index(drop=True)
    

    gives the desired output. In some cases you might want to save the original index and add a second level to index the nested elements, e.g.

    0  0         slim
       1        waist
       2          man
    1  0         slim
       1    waistline
    2  0        santa
    

    If this is what you want, just omit .reset_index(drop=True) from the chain.