我有一个 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_normalize
与 DataFrame.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/