1

I have a list of lists of tuples I would like to transform into a pandas data frame.

Each tuple contains the same amount of elements and they are ordered. How can I load this data into a pandas dataframe so each value individually appears in a cell under it's respective? Original Data Sample:

[[(John, Smith),(Jane, Smith)], [(John, Doe),(Jane, Doe)]]

Desired Output

First| Last
--------------
John | Smith |
Jane | Smith |
John | Doe   |
Jane | Doe   |
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • I'm ok working with a list of tuples, but not a list of lists of tuples. How would you recommend solving to achieve the desired output above? – Cookie Monster Dec 03 '20 at 05:05
  • 1
    Just do this: `pd.DataFrame(l[0], columns=['First', 'Last'])`, where `l` is your `list of list of tuples`. – Mayank Porwal Dec 03 '20 at 05:07
  • 1
    `pd.DataFrame([j for i in tups for j in i], columns=['First', 'Last'])` where `tups=[[("John", "Smith"),("Jane", "Smith")], [("John","Doe"),("Jane", "Doe")]]` – Saravanakumar V Dec 03 '20 at 05:11
  • 1
    `from itertools import chain; pd.DataFrame(chain.from_iterable(l))`. – Henry Yik Dec 03 '20 at 05:28
  • @SaravanakumarV - Thank you. Your explanation is on point and lead me to learn more about list comprehension, which was my initial problem....I knew little about. – Cookie Monster Dec 03 '20 at 18:48

0 Answers0