Home > Software design >  Convert List of tuples to dataframe
Convert List of tuples to dataframe

Time:08-24

Having a data as below:

mylist=[((10, 'L5', ['abc']), 0),
 ((15, 'L6', ['bda', 'LAS']), 5)]

I need to convert this data to data frame. I need the dataframe/output as follows:

COL1 COL2 COL3 COL4
10 L5 ['abc'] 0
15 L6 ['bda', 'LAS'] 5

CodePudding user response:

The data, in its current state, needs a little re-structuring to slot into a DataFrame nicely. To accomplish this, the dataset is iterated where each tuple and the additional value are concatenated into a single tuple. Finally, the list of concatenated tuples is slotted into the DataFrame as shown.

For example:

import pandas as pd

# Original dataset.
data = [((10, 'L5', ['abc']), 0), 
        ((15, 'L6', ['bda', 'LAS']), 5)]

# Re-structure the data into a flattened list of tuples.
data_ = [a   (b, ) for a, b in data]
# Create the DataFrame
df = pd.DataFrame(data_, columns=['COL1', 'COL2', 'COL3', 'COL4'])

Output:

   COL1 COL2        COL3  COL4
0    10   L5       [abc]     0
1    15   L6  [bda, LAS]     5

CodePudding user response:

You could use tuple unpacking in a comprehension:

In [1]: import pandas as pd
In [2]: my_data = [((10, 'L5', ['abc']), 0), ((15, 'L6', ['bda', 'LAS']), 5)]
In [3]: new_my_data = [[col1, col2, col3, col4] for ((col1, col2, col3), col4) in my_data]
In [4]: new_my_data
Out[4]: [[10, 'L5', ['abc'], 0], [15, 'L6', ['bda', 'LAS'], 5]]
In [5]: df = pd.DataFrame(new_my_data, columns=['COL1', 'COL2', 'COL3', 'COL4'])
   ...: df
Out[5]: 
   COL1 COL2        COL3  COL4
0    10   L5       [abc]     0
1    15   L6  [bda, LAS]     5
  • Related