0

I have a list of tuples in (row, column, value) format (coming from sql query). Eg

data = [
    (date_1, brand_x, 5),
    (date_1, brand_y, 8),
    (date_2, brand_x, 12)
]

and I want to convert them to a dataframe

index  | brand_x | brand_y
--------------------------
date_1 |   5     |    8
date_2 |  12     |   NaN

I've been trying with Dataframe.from_records/from_dict and different orient values, but to no avail. What is the correct way to do it?

PS: If there's a way to do it by converting the tuples to dicts first, it's just as well

blue_note
  • 27,712
  • 9
  • 72
  • 90

1 Answers1

3
import pandas as pd
data = [
    ('date_1', 'brand_x', 5),
    ('date_1', 'brand_y', 8),
    ('date_2', 'brand_x', 12)
]    

pd.DataFrame(data).pivot_table(index=0,columns=1)