Python Pandas: Flatten a list of dataframe

Learn, how to flatten a list of pandas dataframe? By Pranit Sharma Last updated : September 19, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

A list is a collection of the heterogeneous type of elements which means the data inside a list can be of any type. A list in python is just like an array where the first element has an index of 1 and the last element has an index n-1 where n is the length of the list.

When we convert or change the data format to a narrow format, it is called flattening. The advantage of the flattened list is increasing the computing speed and helps in better understanding of data and effective data analysis.

Problem statement

Given a Pandas DataFrame, we have to flatten it to a list.

Flattening a list of DataFrame

To flatten a DataFrame into a list, we will first create a DataFrame with a column having a list of multiple elements. We will first loop over this column and then we will again loop over each list of this column to store every element of this list into a separate variable. We will also access the index value of column values which will help us to identify the data.

Finally, we will create another dataframe with all the gained values.

Let us understand with the help of an example,

Python code to flatten a list of pandas dataframe

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'Sales_In_Both_Quarter':[[13450,16347,18920,22039],[10000,9000,8263,1929]],
    'Profit':['Yes','No']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("DataFrame1:\n",df,"\n")

# Accessing values and flattening the list
flatdata = pd.DataFrame([(index, value) for (index, values)
    in df['Sales_In_Both_Quarter'].iteritems() for value in values],
    columns=['index', 'Sales_In_Both_Quarter']).set_index('index')


# merging with previous dataframe
df = df.drop('Sales_In_Both_Quarter', axis=1).join(flatdata)

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Pandas Flatten a list of dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.