Built with Tutify.io

    AIStudyLabs

    Convert Pandas Series of Lists to One Series

    Convert Pandas Series of Lists to One Series

    How to convert pandas series of lists into one single series? Converting a series of lists into a single series is a process of combining the different lists in a series into one single series by using apply(), stack(), explode(), dropna(), and pandas.concat() functions.

    In pandas, a Series is a one-dimensional labeled array capable of holding any data type. Series stores data in sequential order. It is one-column information similar to columns in an excel sheet/SQL table. In this article, I will explain different ways to convert a series of lists into a single series in Pandas.

    Key Points –

    <a id="h-quick-examples-of-convert-series-of-lists-to-one-series"></a>

    Quick Examples of Convert Series of Lists to One Series

    If you are in a hurry, below are some quick examples of converting pandas series of lists to one series.

    
    # Quick examples of convert series of lists to one series
    
    # Example 1: Convert series of list into one series
    ser2 = ser.apply(pd.Series).stack().reset_index(drop = True)
    
    # Example 2: Use explode() function
    ser2 = ser.explode()
    
    # Example 3: Use explode() & dropna() method
    ser2 = ser.explode().dropna()  
    
    # Example 4: Use extend() method
    slist =[]
    for x in ser:
        slist.extend(x)
    ser1 = pd.Series(slist)
    
    # Example 5: Using the extend() method  
    slist = [st for row in ser for st in row]   
    ser1 = pd.Series(slist)
                  
    # Example 6: Use apply() method with convert multiple series lists of integers
    ser = pd.Series([[3, 5, 6, 8], [2, 4, 3, 7],[1, 8, 9, 5]])
    ser2 = ser.apply(pd.Series).stack().reset_index(drop = True)
    
    # Example 7: Use concat() method
    slist =[]
    for x in ser:
        slist.append(pd.Series(x))
    ser1 = pd.concat([*slist]).reset_index(drop = True)
    

    <a id="h-create-pandas-series"></a>

    Create Pandas Series

    Pandas Series is a one-dimensional, Index-labeled data structure that is available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

    Note : Series data structure is the same as the NumPy array data structure but only one difference that is arrays indices are integers and starts with 0, whereas in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.

    Now, let’s create a series of lists,

    
    import pandas as pd
      
    # Create a Pandas series of lists
    ser = pd.Series([["Spark","PySpark","Python"],
                     ["Pandas","Python","oracle"],
                     ["java","NumPy","R"]])
    print(ser)
    

    Yields below output. Note that in this example we have 3 lists in a single series object.

    
    # Output:
    0    [Spark, PySpark, Python]
    1    [Pandas, Python, oracle]
    2            [java, NumPy, R]
    dtype: object
    

    <a id="h-convert-series-of-lists-into-single-series-in-pandas"></a>

    Convert Series of Lists into Single Series in Pandas

    In pandas converting a series of lists into one series is nothing but merging multiple lists into one single list. This process will be done by stack() method. We will use the following syntax to convert the multiple series of lists into a single list. For example, Series.apply(Pandas.Series).stack().reset_index(drop=True).

    
    # Convert series of list into one series
    ser2 = ser.apply(pd.Series).stack().reset_index(drop = True)
    print(ser2)
    

    Yields below output. Here, apply(pd.Series) converts to DataFrame and stack() converts it back to single Series with index. The reset_index() method is used to drop the index from the Series.

    
    # Output:
    0      Spark
    1    PySpark
    2     Python
    3     Pandas
    4     Python
    5     oracle
    6       java
    7      NumPy
    8          R
    dtype: object
    

    <a id="h-use-explode-method"></a>

    Use explode() Method

    We can also use explode() method to convert a Series with multiple lists into one single list in Pandas.

    
    # Use explode() function
    ser2 = ser.explode()
    print(ser2)
    

    Yields the same output as above.

    <a id="h-use-explode-amp-dropna-method"></a>

    Use explode() & dropna() Method

    When we take an empty list as one of the lists of series, then apply explode() method with that series it will return the NaN value as one of the values of the series.

    
    # Creating a Pandas series of lists
    ser = pd.Series([["Spark","PySpark","Python"],
                     ["Pandas","Python","oracle"],
                     ["java","NumPy","R"],
                     []])
      
    # Use explode() method
    ser2 = ser.explode()     
    print(ser2)
    
    # Output:
    # 0      Spark
    # 0    PySpark
    # 0     Python
    # 1     Pandas
    # 1     Python
    # 1     oracle
    # 2       java
    # 2      NumPy
    # 2          R
    # 3        NaN
    dtype: object
    

    Using dropna() method we can drop the NaN values of the series. Let's use dropna() method it will drop the NaN values of the series.

    
    # Use explode() & dropna() method
    ser2 = ser.explode().dropna()    
    print(ser2)
    

    Yields below output.

    
    # Output:
    0      Spark
    0    PySpark
    0     Python
    1     Pandas
    1     Python
    1     oracle
    2       java
    2      NumPy
    2          R
    dtype: object
    

    <a id="h-use-extend-method"></a>

    Use extend() Method

    We can also use extend() method to iterate over each element of the Series and add it to a list.

    
    # Use extend() method
    slist =[]
    for x in ser:
        slist.extend(x)
    ser1 = pd.Series(slist)
    print(ser1)
    
    # using the extend() method  
    slist = [st for row in ser for st in row]   
    ser1 = pd.Series(slist)
    print(ser1)
    

    Yields the same output as above.

    <a id="h-convert-multiple-series-lists-of-integers"></a>

    Convert Multiple Series Lists of Integers

    Converting multiple series lists of integers can be done by combining the multiple lists into one single list integer. This process will be done by apply() & stack() methods. The reset_index() method is used to create the index of the DataFrame/Series.

    
    import pandas as pd
      
    # Create a Pandas series of lists
    ser = pd.Series([[3, 5, 6, 8], [2, 4, 3, 7],[1, 8, 9, 5]])
                  
    # Use apply() method with convert multiple series lists of integers
    ser2 = ser.apply(pd.Series).stack().reset_index(drop = True)
    print(ser2)
    

    Yields below output.

    
    # Output:
    0     3
    1     5
    2     6
    3     8
    4     2
    5     4
    6     3
    7     7
    8     1
    9     8
    10    9
    11    5
    dtype: int64
    

    <a id="h-use-concat-method"></a>

    Use concat() Method

    We can also iterate through each list in the series and concatenate them using append() and finally use concat() to convert them to a list.

    
    # Use concat() method
    slist =[]
    for x in ser:
        slist.append(pd.Series(x))
     
    ser1 = pd.concat([*slist]).reset_index(drop = True)
    print(ser1)
    

    Yields the same output as above.

    <a id="h-faq-on-convert-pandas-series-of-lists-to-one-series"></a>

    FAQ on Convert Pandas Series of Lists to One Series

    A Pandas Series of Lists is a series where each element is a list. This can occur when a column in a DataFrame contains lists or arrays as its values.

    The explode() method will handle lists of different lengths by expanding them into separate rows. Missing values (NaNs) will be generated for shorter lists.

    The explode() method works only one level deep. If your Series contains nested lists (e.g., [[1, 2], [3, 4]]), you might need to apply explode() multiple times or use a custom function to flatten them fully.

    You can apply other transformations while flattening a Series of lists! You can use functions like map(), apply(), or astype() to modify the elements of the lists before or after flattening.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to convert a series of lists to a single series in Pandas using concat(), apply(), stack(), explode(), and dropna() functions with examples.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References