Python 如何从包含所有值I';我想在嵌套在循环列表元素中的数据框中包含?
列表如下所示Python 如何从包含所有值I';我想在嵌套在循环列表元素中的数据框中包含?,python,pandas,dataframe,indexing,python-3.7,Python,Pandas,Dataframe,Indexing,Python 3.7,列表如下所示 [ { "legal": [ { "importance": 3, "notes": "", "updated": 19976, "date": 09/05/2020 } ] },
[
{
"legal": [
{
"importance": 3,
"notes": "",
"updated": 19976,
"date": 09/05/2020
}
]
},
{
"legal": [
{
"importance": 3,
"notes": "",
"updated": 15783,
"date": 09/05/2020
}
]
}
]
| date *index | importance | notes | updated |
|-------------|------------|-------|---------|
| 09/05/2020 | 3 | NaN | 19976 |
| | 3 | NaN | 15783 |
我希望列为“重要性”、“注释”、“更新”和“日期”。我想把索引设为“日期”。我似乎找不到一种方法,用列表中的“合法”元素来制作熊猫数据框。我不必将它们包含在数据帧中。这就是我接收数据的方式。我想知道制作熊猫数据帧的正确方法。让它看起来像这样
[
{
"legal": [
{
"importance": 3,
"notes": "",
"updated": 19976,
"date": 09/05/2020
}
]
},
{
"legal": [
{
"importance": 3,
"notes": "",
"updated": 15783,
"date": 09/05/2020
}
]
}
]
| date *index | importance | notes | updated |
|-------------|------------|-------|---------|
| 09/05/2020 | 3 | NaN | 19976 |
| | 3 | NaN | 15783 |
您可以尝试下面的列表理解来获取每个字典的
legal
键和第一个(唯一的)字典,然后我将空字符串替换为NaN
:
df = pd.DataFrame([i['legal'][0] for i in lst]).replace('', np.nan)
然后:
print(df)
将提供:
date importance notes updated
0 09/05/2020 3 NaN 19976
1 09/05/2020 3 NaN 15783
用于此
[33]中的pd.json\u规范化(数据,'legal')。替换({'':np.nan})。设置索引('date')
出[33]:
更新重要须知
日期
2020年5月9日3南19976
2020年5月9日3南15783