[英]Assign data in JSON file to a variable based on condition python
我正在嘗試根據日期代表的季度從 JSON 文件中獲取數據。 我的目標是將數據分配給一個變量,所以我應該有 Q1、Q2、Q3、Q4 變量來保存數據。 下面是JSON:
{
"lastDate":{
"0":"2022Q4",
"1":"2022Q4",
"2":"2022Q4",
"7":"2022Q4",
"8":"2022Q4",
"9":"2022Q4",
"18":"2022Q3",
"19":"2022Q3",
"22":"2022Q3",
"24":"2022Q2"
},
"transactionType":{
"0":"Sell",
"1":"Automatic Sell",
"2":"Automatic Sell",
"7":"Automatic Sell",
"8":"Sell",
"9":"Automatic Sell",
"18":"Automatic Sell",
"19":"Automatic Sell",
"22":"Automatic Sell",
"24":"Automatic Sell"
},
"sharesTraded":{
"0":"20,200",
"1":"176,299",
"2":"8,053",
"7":"167,889",
"8":"13,250",
"9":"176,299",
"18":"96,735",
"19":"15,366",
"22":"25,000",
"24":"25,000"
}
}
現在,如果我嘗試使用以下代碼:
import json
data = json.load(open("AAPL22data.json"))
Q2data = [item for item in data if '2022Q2' in data['lastDate']]
print(Q2data)
我理想中的output應該是:
{
"lastDate":{
"24":"2022Q2"
},
"transactionType":{
"24":"Automatic Sell"
},
"sharesTraded":{
"24":"25,000"
}
}
然后對其他區域重復相同的結構。 但是,我當前的 output 給了我“[]”
使用 pandas,您可以閱讀此嵌套字典並將其轉換為表格表示形式。 那么你需要的聚合就變得很自然了。
import pandas as pd
sample_dict = {
"lastDate":{
"0":"2022Q4",
"1":"2022Q4",
"2":"2022Q4",
"7":"2022Q4",
"8":"2022Q4",
"9":"2022Q4",
"18":"2022Q3",
"19":"2022Q3",
"22":"2022Q3",
"24":"2022Q2"
},
"transactionType":{
"0":"Sell",
"1":"Automatic Sell",
"2":"Automatic Sell",
"7":"Automatic Sell",
"8":"Sell",
"9":"Automatic Sell",
"18":"Automatic Sell",
"19":"Automatic Sell",
"22":"Automatic Sell",
"24":"Automatic Sell"
},
"sharesTraded":{
"0":"20,200",
"1":"176,299",
"2":"8,053",
"7":"167,889",
"8":"13,250",
"9":"176,299",
"18":"96,735",
"19":"15,366",
"22":"25,000",
"24":"25,000"
}
}
print(pd.DataFrame.from_dict(sample_dict))
回報
Output:
lastDate transactionType sharesTraded
0 2022Q4 Sell 20,200
1 2022Q4 Automatic Sell 176,299
2 2022Q4 Automatic Sell 8,053
7 2022Q4 Automatic Sell 167,889
8 2022Q4 Sell 13,250
9 2022Q4 Automatic Sell 176,299
18 2022Q3 Automatic Sell 96,735
19 2022Q3 Automatic Sell 15,366
22 2022Q3 Automatic Sell 25,000
24 2022Q2 Automatic Sell 25,000
那么一個簡單的group_by就可以解決問題。
使用字典理解:
import json
my_json = """{
"lastDate":{
"0":"2022Q4",
"1":"2022Q4",
"2":"2022Q4",
"7":"2022Q4",
"8":"2022Q4",
"9":"2022Q4",
"18":"2022Q3",
"19":"2022Q3",
"22":"2022Q3",
"24":"2022Q2"
},
"transactionType":{
"0":"Sell",
"1":"Automatic Sell",
"2":"Automatic Sell",
"7":"Automatic Sell",
"8":"Sell",
"9":"Automatic Sell",
"18":"Automatic Sell",
"19":"Automatic Sell",
"22":"Automatic Sell",
"24":"Automatic Sell"
},
"sharesTraded":{
"0":"20,200",
"1":"176,299",
"2":"8,053",
"7":"167,889",
"8":"13,250",
"9":"176,299",
"18":"96,735",
"19":"15,366",
"22":"25,000",
"24":"25,000"
}
}"""
data = json.loads(my_json)
var = "24" #This corresponds to 2022 Q2 in your example
data = {k:{var: v[var]} for k, v in data.items()}
data = json.dumps(data, indent = 2)
print(data)
Output:
{
"lastDate": {
"24": "2022Q2"
},
"transactionType": {
"24": "Automatic Sell"
},
"sharesTraded": {
"24": "25,000"
}
}
感謝@FrancoMilanese 提供有關 Pandas group_by
的信息,以下是答案:
import json
import pandas as pd
data = json.load(open("AAPL22data.json"))
df = pd.DataFrame.from_dict(data)
q2df = df.groupby('lastDate')
q2df.get_group('2022Q2') #change '2022q2' for others & assign to a different variable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.