'How to turn a list of lists into columns of a pandas dataframe?

I would like to ask how I can unnest a list of list and turn it into different columns of a dataframe. Specifically, I have the following dataframe where the Route_set column is a list of lists:

   Generation                              Route_set
0           0  [[20. 19. 47. 56.] [21. 34. 78. 34.]]

The desired output is the following dataframe:

   route1  route2
0      20      21
1      19      34
2      47      78
3      56      34

Any ideas how I can do it? Thank you in advance!



Solution 1:[1]

You can create a dictionnary and update it using a for loop, not the fastest way but pretty easy.

new_dic = {}
# Create and fill dictionnary, each key_value pair corresponds to a list
for i, values in enumerate(df.Route_set):
    new_dic[f'route{i}'] = values
# Drop the double list column
df.drop('Route_set', axis=1, inplace=True)
# Updated dataframe with dic key_value pairs
for key in new_dic.keys():
    df[key] = new_dic[key]

You can probably do better, but this should be ok for a quick fix to your issue !

Solution 2:[2]

I made a solution that created a NumPy array(), transposes it and converts it back to a list of lists using tolist():

import numpy as np
import pandas as pd

routes = {
    "Generation": 0,
    "Route_set": [[[20, 19, 47, 56], [21, 34, 78, 34]]]
}

array = np.array(routes["Route_set"][0]).T.tolist()

columns_name = [f"routes{i}" for i in range(1, len(array[0])+1)]
df = pd.DataFrame(data=array, columns=columns_name)

print(df)

Outputs:

   route1  route2
0      20      21
1      19      34
2      47      78
3      56      34

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 arlaine
Solution 2