Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten a list of elements in Pandas DataFrame

My data-structure is:

ds = [{
    "name": "groupA",
    "subGroups": [123,456]
},
{
    "name": "groupB",
    "subGroups": ['aaa', 'bbb' , 'ccc']
}]

This gives the following dataframe

df = pd.DataFrame(ds)

    name    subGroups
0   groupA  [123, 456]
1   groupB  [aaa, bbb, ccc]   

I want:

    name    subGroupsFlattend
0   groupA  123
1   groupA  456
2   groupB  aaa
3   groupB  bbb
4   groupB  ccc

Any ideas?

like image 453
More Than Five Avatar asked Mar 23 '18 14:03

More Than Five


People also ask

How do you flatten a list in a DataFrame?

The first method to flatten the pandas dataframe is through NumPy python package. There is a function in NumPy that is numpy. flatten() that perform this task. First, you have to convert the dataframe to numpy using the to_numpy() method and then apply the flatten() method.

How do you flatten a list of lists?

Flatten List of Lists Using itertools (chain()) This approach is ideal for transforming a 2-D list into a single flat list as it treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument in a sequential manner.

How do you flatten a column list in Python?

Use as flatten_col(input, 'B', 'B') in your example. The benefit of this method is that copies along all other columns as well (unlike some other solutions).

What is the flatten method in pandas?

Return a copy of the array collapsed into one dimension. Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran ordering from a . The default is 'C'.


2 Answers

Use explode:

df = df.explode('subGroups')
like image 154
Jake Reece Avatar answered Sep 17 '22 13:09

Jake Reece


You can fix your output by following :

pd.DataFrame({'name':df.name.repeat(df.subGroups.str.len()),'subGroup':df.subGroups.sum()})
Out[364]: 
     name subGroup
0  groupA      123
0  groupA      456
1  groupB      aaa
1  groupB      bbb
1  groupB      ccc
like image 43
BENY Avatar answered Sep 17 '22 13:09

BENY