List of LISTS of tuples to Pandas dataframe? List of LISTS of tuples to Pandas dataframe? python-3.x python-3.x

List of LISTS of tuples to Pandas dataframe?


Just flatten your list into a list of tuples (your initial list contains a sublists of tuples):

In [1251]: tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]In [1252]: pd.DataFrame([t for lst in tupList for t in lst])Out[1252]:            0             1     20  commentID   commentText  date1     123456  blahblahblah  20192      45678   hello world  20183          0          text  2017


tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]print(pd.DataFrame(sum(tupList,[])))

Output

           0             1     20  commentID   commentText  date1     123456  blahblahblah  20192      45678   hello world  20183          0          text  2017


A shorter code this:

from itertools import chainimport pandas as pdtupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]new_list = [x for x in chain.from_iterable(tupList)]df = pd.DataFrame.from_records(new_list)

Edit

You can make the list comprehension directly in the from_records function.