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 |
Advertisement
Answer
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