我想创建一个新列表,其中包含一个大型嵌套dict中的项。
下面是嵌套dict的一个片段:
AcceptedAnswersPython_combined.json
{
"items": [
{
"answers": [
{
"creation_date": 1533083368,
"is_accepted": false
},
{
"creation_date": 1533083567,
"is_accepted": false
},
{
"creation_date": 1533083754,
"is_accepted": true
},
{
"creation_date": 1533084669,
"is_accepted": false
},
{
"creation_date": 1533089107,
"is_accepted": false
}
],
"creation_date": 1533083248,
"tags": [
"python",
"pandas",
"dataframe"
]
},
{
"answers": [
{
"creation_date": 1533084137,
"is_accepted": true
}
],
"creation_date": 1533083367,
"tags": [
"python",
"binary-search-tree"
]
}
]
} 新的列表应该包含每个项目的creation_date,就像在answers列表中有切块一样多。因此,如果出现上面的代码片段,新列表应该如下所示:
question_date_per_answer = [[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]]我需要这个新列表的原因是,我想确定每个answers creation_date与其相关的问题creation_date(在items dict中说明)之间的差异。
这张新的熊猫名单应该是这样的:
question creation date answer creation date
0 1533083248 1533083368
1 1533083248 1533083567
2 1533083248 1533083754
3 1533083248 1533084669
4 1533083248 1533089107
5 1533083367 1533084137我可以像这样反复回答所有的问题:
items = json.load(open('AcceptedAnswersPython_combined.json'))['items']
question_creation_date = [item['creation_date'] for item in items]但这给我留下了一个列表,它不等于answers creation_date的数量。
我不能把头绕着这件事。
那么,我如何创建这样一个列表,其中问题创建日期的数量等于答案创建日期的数量?(如question_date_per_answer)
提前谢谢。
发布于 2018-10-06 11:18:46
您需要对项“答案”进行迭代,然后在oreder中为每个答案获取creation_date,以获得答案创建日期。
my_json = """{
"items": [
{
"answers": [
{
"creation_date": 1533083368,
"is_accepted": false
},
{
"creation_date": 1533083567,
"is_accepted": false
},
{
"creation_date": 1533083754,
"is_accepted": true
},
{
"creation_date": 1533084669,
"is_accepted": false
},
{
"creation_date": 1533089107,
"is_accepted": false
}
],
"creation_date": 1533083248,
"tags": [
"python",
"pandas",
"dataframe"
]
},
{
"answers": [
{
"creation_date": 1533084137,
"is_accepted": true
}
],
"creation_date": 1533083367,
"tags": [
"python",
"binary-search-tree"
]
}
]
}"""
import json
data = json.loads(my_json)
dates = [(question["creation_date"], answer["creation_date"])
for question in data["items"] for answer in question["answers"]]
print(dates)发布于 2018-10-06 11:28:09
您仍然可以使用手头的列表。
让我们试着从你已经拥有的列表中做一个数据-
l = [[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]]
df = pd.DataFrame(l)不幸的是你得到了以下-
0 1 2 3 4
0 1533083248 1.533083e+09 1.533083e+09 1.533083e+09 1.533083e+09
1 1533083367 NaN NaN NaN NaN所以我们得把它转过来。为此,让我们做以下几件事-
from itertools import zip_longest
k = list(list(zip_longest(*l))) #Unless the list will be truncated to the length of shortest list.
df = pd.DataFrame(k)产出-
0 1
0 1533083248 1.533083e+09
1 1533083248 NaN
2 1533083248 NaN
3 1533083248 NaN
4 1533083248 NaN现在,我们将使用前面的值by - df.fillna(method='ffill')来填充NaNs。
整个片段-
from itertools import zip_longest
l=[1533083248, 1533083248, 1533083248 , 1533083248, 1533083248], [1533083367]
k=list(list(zip_longest(*l)))
df = pd.DataFrame(k)
df.fillna(method='ffill')瞧-
0 1
0 1533083248 1.533083e+09
1 1533083248 1.533083e+09
2 1533083248 1.533083e+09
3 1533083248 1.533083e+09
4 1533083248 1.533083e+09https://stackoverflow.com/questions/52678242
复制相似问题