Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Partially exploding dataframe with nested lists items

I have a dataframe with the below format:

         A              B              C
0  [[1,2],[3,4]]  [[5,6],[7,8]]  [[9,10],[11,12]]

for multiple rows.
The sub lists are always of length 2 and the length of A,B,C lists are always the same size.However the lengths of the latter vary and for example can be of size 2 or 6..etc for different rows.

What i would like to do is to explode rows like these into:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

         A              B              C
0      [1,2]          [5,6]          [9,10]
0      [3,4]          [7,8]          [11,12]

>Solution :

Assuming you really have lists of lists, a simple explode on all columns should work:

df.explode(df.columns.to_list())

output:

        A       B         C
0  [1, 2]  [5, 6]   [9, 10]
0  [3, 4]  [7, 8]  [11, 12]

used input:

df = pd.DataFrame([[[[1,2],[3,4]], [[5,6],[7,8]], [[9,10],[11,12]]]],
                  columns=['A', 'B', 'C'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading