Making A New Row Using Each Element Of A Sequence Present In A DataFrame Column

Overview:

  • A DataFrame column can have a Python sequence such as tuple, list as an element.
  • Each element from such sequences can be made a row, using the explode() instance method, with values from other columns assuming the same values in the new row.

Example:

# Example Python program that splits a Python sequence which

# is an element of a DataFrame into separate elements

import pandas as pds

 

aList           = [1, 2, 3, 4];

aTuple          = ("a", "b", "c", "d");

 

# Create a DataFrame                  

aDataFrame = pds.DataFrame([(aList, 100),

                            (aTuple, 200),

                            (30, 300),

                            ([], 400)], columns = ["X", "Y"]);

                          

print("Contents of the original DataFrame");

print(aDataFrame);

 

# Explode the column X

newDataFrame = aDataFrame.explode("X");

 

print("Contents of the DataFrame after exploding the column X:");

print(newDataFrame);                        

 

Output:

Contents of the original DataFrame

              X    Y

0  [1, 2, 3, 4]  100

1  (a, b, c, d)  200

2            30  300

3            []  400

Contents of the DataFrame after exploding the column X:

     X    Y

0    1  100

0    2  100

0    3  100

0    4  100

1    a  200

1    b  200

1    c  200

1    d  200

2   30  300

3  NaN  400

 


Copyright 2022 © pythontic.com