如何从 3 个元素元组的列表创建 Pandas 数据框?

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

我正在尝试从具有 3 个元素的元组列表构建数据框,看起来像 [(name, date, score), ... ].

我想将名称作为索引列,将日期作为列 headers,分数是数据。以下是我所做的。

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]

它成功了,但由于数据集很大,需要一段时间。有没有更好的构造方法?

这是 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