基于条件python将JSON文件中的数据分配给变量

bt1cpqcv  于 2022-12-05  发布在  Python
关注(0)|答案(3)|浏览(169)

我尝试根据日期代表的季度从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)

我的理想输出应该是:

{
    "lastDate":{
        "24":"2022Q2"
    },
    "transactionType":{
        "24":"Automatic Sell"
    },
    "sharesTraded":{
        "24":"25,000"
    }
}

然后对其他季度重复相同的结构。但是,我的当前输出给我“[ ]”

vh0rcniy

vh0rcniy1#

有了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就可以了。

5lwkijsr

5lwkijsr2#

使用 * 字典理解 *:
第一个
输出量:

{
  "lastDate": {
    "24": "2022Q2"
  },
  "transactionType": {
    "24": "Automatic Sell"
  },
  "sharesTraded": {
    "24": "25,000"
  }
}
ar7v8xwq

ar7v8xwq3#

感谢@FrancoMilanese提供关于Pandasgroup_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

相关问题