convert tuple list of list to dataframe

Question:

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
Asked By: lucky1928

||

Answers:

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}]
Answered By: I'mahdi
df = pd.DataFrame(map(dict, data))
Answered By: michotross
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.