Unpack List in Column Pandas : Various Methods

Unpack List in Column using Pandas Various Methods

Pandas is a popular Python package that allows you to manipulate the dataframe easily. It has many inbuilt functions to handle and transform data efficiently. Suppose you have a list of lists in a particular column then how you can separate all elements of a list as a separate column? In this post, you will learn how to unpack a list in a column in pandas.

Sample Data

Before going to the coding demonstration let’s first create a sample data that will be used to unpack the list. Suppose I have dataframe that has names and scores as columns. The scores column contain a list of three scores for each individual.

Below are the lines of code for the creation of sample data.

import pandas as pd

data = {
    'Name': ['John', 'Maya', 'Peter'],
    'Scores': [[85, 90, 95], [91, 88 , 93], [78, 80 , 85]]
}

df = pd.DataFrame(data)
print(df)

Output

sample data to unpack the list in pandas
sample data to unpack the list in pandas

Methods to Unpack List in Column Pandas

In this section, you will know all the methods to unpack the list in a column. Just follow it for a deep understanding.

Method 1  : Using the apply(pd.Series) functin

In this method, you will apply a function to each row of the column using the apply() function. The apply() function will use the pd.Series() as an argument that will convert each list of the list to a series. After that, you will also delete the scores columns to make the data relevant.

Run the below lines of code.

import pandas as pd

data = {
    'Name': ['John', 'Maya', 'Peter'],
    'Scores': [[85, 90, 95], [91, 88 , 93], [78, 80 , 85]]
}

df = pd.DataFrame(data)

df[['Score 1', 'Score 2', 'Score 3']] = df['Scores'].apply(pd.Series)
df = df.drop('Scores', axis=1)
print(df)

Output

unpacking list using the apply() function
unpacking list using the apply() function

Method 2: Using the DataFrane() constructor with tolist() function

The second method is the use of the DataFrame() constructor with the tolist() function. Here you will convert the column to the list and pass it as an argument to the constructor.

Execute the below lines of code.

import pandas as pd

data = {
    'Name': ['John', 'Maya', 'Peter'],
    'Scores': [[85, 90, 95], [91, 88 , 93], [78, 80 , 85]]
}

df = pd.DataFrame(data)

df[['Score 1 ', 'Score 2', 'Score 3']] = pd.DataFrame(df['Scores'].tolist(), index=df.index)
df.drop('Scores', axis=1, inplace=True)

print(df)

Output

unpacking list using the DataFrame() constructor
unpacking list using the DataFrame() constructor

Conclusion

The Unpack List in Column Pandas is a common requirement for data analysis. The above methods will unpack the list in a column in pandas. In this tutorial, I have demonstrated the two methods to unpack the list. You can use any one of them at per convenience.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner