How to use pandas.series with lambda function In Python Pandas
An anonymous function that can take any number of arguments, but can only have one expression is known as lambda function. In this method, we will see how we can unnest multiple list columns using the pandas.series with lambda function.
Syntax:
df=df.set_index(‘Name’).apply(lambda x: x.apply(pd.Series).stack()).reset_index().drop(‘level_1’, 1)
Here,
- column-3: It is the column that is already unnested.
- df: It is the data frame that has those nested columns.
Implementations:
In this example, we have created a dataset, which has three columns, Name, Favourite Ice-cream and Favourite Soft-Drink, out of which Favourite Ice-cream and Favourite Soft-Drink columns are nested. We have unnested those columns using pandas.series with lambda function.
Python3
# Import the Pandas library import pandas as pd # Create a data frame that has nested columns df = pd.DataFrame({ 'Name' : [ 'Arun' , 'Aniket' , 'Ishita' , 'Raghav' , 'Vinayak' ], 'Favourite Ice-cream' :[[ 'Strawberry' , 'Choco-chips' ], [ 'Vanilla' , 'Black Currant' ], [ 'Butterscotch' , 'Chocolate' ], [ 'Mango' , 'Choco-chips' ], [ 'Kulfi' , 'Kaju-Kishmish' ]], 'Favourite Soft-Drink' :[[ 'Coca Cola' , 'Lemonade' ], [ 'Thumbs Up' , 'Sprite' ], [ 'Moutain Dew' , 'Fanta' ], [ 'Mirinda' , 'Maaza' ], [ '7Up' , 'Sprite' ]]}) # Print the actual data frame print ( 'Actual dataframe:\n' ,df) # Unnest the nested columns df = df.set_index( 'Name' ). apply ( lambda x: x. apply (pd.Series).stack()).reset_index().drop( 'level_1' , 1 ) # Print the unnested data frame print ( '\nDataframe after unnesting:\n' ,df) |
Output:
Actual dataframe:
Name Favourite Ice-cream Favourite Soft-Drink
0 Arun [Strawberry, Choco-chips] [Coca Cola, Lemonade]
1 Aniket [Vanilla, Black Currant] [Thumbs Up, Sprite]
2 Ishita [Butterscotch, Chocolate] [Moutain Dew, Fanta]
3 Raghav [Mango, Choco-chips] [Mirinda, Maaza]
4 Vinayak [Kulfi, Kaju-Kishmish] [7Up, Sprite]
Dataframe after unnesting:
Name Favourite Ice-cream Favourite Soft-Drink
0 Arun Strawberry Coca Cola
1 Arun Choco-chips Lemonade
2 Aniket Vanilla Thumbs Up
3 Aniket Black Currant Sprite
4 Ishita Butterscotch Moutain Dew
5 Ishita Chocolate Fanta
6 Raghav Mango Mirinda
7 Raghav Choco-chips Maaza
8 Vinayak Kulfi 7Up
9 Vinayak Kaju-Kishmish Sprite
Unnest (Explode) Multiple List Columns In A Pandas Dataframe
An open-source manipulation tool that is used for handling data is known as Pandas. Have you ever encountered a dataset that has columns with data as a list? In such cases, there is a necessity to split that column into various columns, as Pandas cannot handle such data. In this article, we will discuss the same, i.e., unnest or explode multiple list columns into a Pandas data frame.
Contact Us