python - 以矢量化方式将复杂列表转换为数据帧

标签 python pandas

我有一个 len ~ 1M 的嵌套列表。下面的片段。我希望将其转换为数据框,如下所示。

import pandas as pd

list_of_data = [{'id':1, 'name': 'A', 'results':{'test_date':['2020-06-29', '2020-07-02', '2020-07-05', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-13'], 'Score': [12, 23, 23, 12, 11, 13, 13]}},
{'id':2, 'name': 'B', 'results':{'test_date':['2020-06-29', '2020-07-02', '2020-07-05', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-13'], 'Score': [12, 23, 23, 12, 11, 13, 13]}}]

预期数据框:

df = pd.DataFrame({'id':[1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2], 'test_date':['2020-06-29', '2020-07-02', '2020-07-05', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-13', '2020-06-29', '2020-07-02', '2020-07-05', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-13'], 'Score': [12, 23, 23, 12, 11, 13, 13, 12, 23, 23, 12, 11, 13, 13]})

Is it possible to do this?

最佳答案

使用列表理解并传递给 DataFrame 构造函数:

L = [(x['id'], y, z) for x in list_of_data for y, z in 
                                    zip(x['results']['test_date'], x['results']['Score'])]
 
df = pd.DataFrame(L, columns=['id','test_date','Score'])   
print (df)
    id   test_date  Score
0    1  2020-06-29     12
1    1  2020-07-02     23
2    1  2020-07-05     23
3    1  2020-07-09     12
4    1  2020-07-10     11
5    1  2020-07-11     13
6    1  2020-07-13     13
7    2  2020-06-29     12
8    2  2020-07-02     23
9    2  2020-07-05     23
10   2  2020-07-09     12
11   2  2020-07-10     11
12   2  2020-07-11     13
13   2  2020-07-13     13

或者json_normalizeDataFrame.explode重命名列并按列表过滤列:

df = (pd.json_normalize(list_of_data)
        .explode(['results.test_date','results.Score'])
        .rename(columns={'results.test_date':'test_date','results.Score':'Score'})
        [['id','test_date','Score']])

关于python - 以矢量化方式将复杂列表转换为数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73982721/

相关文章:

python - 如何将数据框中的值更改为值列表?

python - 如何使用不同列中的值和文本注释分组条

python - 在 pandas 中,如何识别具有共同值的记录并替换其中一个的值以匹配另一个?

python - 继续系列,索引增加为现有系列

python - JSON pretty-print 在 Python 中不起作用

python - smtplib 是纯 python 还是用 C 实现的?

python - 使用 BeautifulSoup 提取相似的 XML 属性

python - 尝试在 python 中绘制颜色图

python - 如何确定黑盒是多项式还是指数

python - 数据帧中的多 numpy 数组