• Post author:
  • Post category:Pandas
  • Post last modified:November 4, 2024
  • Reading time:12 mins read
You are currently viewing 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.

Advertisements

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 –

  • Flattening a Series of lists involves converting nested lists into individual rows in a single, continuous Series.
  • explode() works on each element of the Series, expanding list entries into separate rows, while keeping the index structure intact.
  • Using pd.Series.explode() is useful for un-nesting data in a Series of lists, which can simplify data manipulation and analysis.
  • Combining explode() with reset_index() can help if you need a continuous index after flattening the Series.
  • The apply(pd.Series).stack() combination provides a flexible alternative, especially when handling irregularly sized lists within the Series.
  • List comprehension with extend() or nested loops can also convert Series of lists, providing control over processing but may require more memory.

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)

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

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

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.

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

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.

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

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.

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 !!

References