Home > Software design >  convert tuple list of list to dataframe
convert tuple list of list to dataframe

Time:09-29

What's proper way to convert list of list of tuple to dataframe? e.g.

data = [
[('A',1),('B',2)],
[('A',2),('B',3)],
[('A',3),('B',4)],
]

I would like to get a dataframe as below:

  A B
  1 2
  2 3
  3 4

CodePudding user response:

df = pd.DataFrame(map(dict, data))

CodePudding user response:

You can create a list of dicts from list of tuples and then create dataframe.

df = pd.DataFrame(dict(lst) for lst in data)
# Or
# df = pd.DataFrame(map(dict,data))
print(df)

Output:

   A  B
0  1  2
1  2  3
2  3  4

Explanation:

>>> list(map(dict, data))
[{'A': 1, 'B': 2}, {'A': 2, 'B': 3}, {'A': 3, 'B': 4}]
  • Related