Home > database >  How to create Pandas dataframe from list of 3 element tuples?
How to create Pandas dataframe from list of 3 element tuples?

Time:11-13

I am trying to construct a dataframe from a list of tuples which has 3 elements, that looks like [(name, date, score), ... ].

I would like to have name as index column and date as column headers and score is the data. Following is what I did.

df = pd.DataFrame({'name':list({x[0] for x in data})}).set_index('name')
date_list = list({x[1] for x in data})
date_list.sort()

df = df.reindex(columns = date_list)

for x in data:
    df.loc[x[0], x[1]] = x[2]

It worked, but due to large data set, it took a while. Is there a better way to construct it?

CodePudding user response:

This is a use case for pivot:

In [1]: import pandas as pd

In [2]: from datetime import date, timedelta

In [3]: today = date.today()

In [4]: data = [("Andrew", today, 100), ("Yixing", today, 105), ("Bam", today   timedelta(days=1), 93
   ...: )]

In [5]: data
Out[5]:
[('Andrew', datetime.date(2021, 11, 11), 100),
 ('Yixing', datetime.date(2021, 11, 11), 105),
 ('Bam', datetime.date(2021, 11, 12), 93)]

In [17]: df = pd.DataFrame(data, columns=["name", "date", "score"])

In [18]: df
Out[18]:
     name        date  score
0  Andrew  2021-11-11    100
1  Yixing  2021-11-11    105
2     Bam  2021-11-12     93

In [23]: df.pivot(index="name", columns="date")
Out[23]:
            score
date   2021-11-11 2021-11-12
name
Andrew      100.0        NaN
Bam           NaN       93.0
Yixing      105.0        NaN
  • Related