Python:嵌套json到 Dataframe

brqmpdu1  于 2023-02-10  发布在  Python
关注(0)|答案(3)|浏览(140)

我正在尝试用python把一个嵌套的json内容转换成一个 Dataframe :

{
       "end_date":"2023-02-02-00:00",
       "price":{
          "2023-01-30":{
             "CHFEUR":{
                "close":0.99612,
                "high":0.99939,
                "low":0.99408,
                "open":0.99925
             },
             "CHFUSD":{
                "close":1.08098,
                "high":1.08884,
                "low":1.08041,
                "open":1.08579
             },
             "EURUSD":{
                "close":1.08518,
                "high":1.0914,
                "low":1.08393,
                "open":1.08609
             }
          },
          "2023-01-31":{
             "CHFEUR":{
                "close":1.00489,
                "high":1.00532,
                "low":0.99497,
                "open":0.99684
             },
             "CHFUSD":{
                "close":1.09152,
                "high":1.09269,
                "low":1.0769,
                "open":1.08127
             },
             "EURUSD":{
                "close":1.08626,
                "high":1.0875,
                "low":1.08022,
                "open":1.08498
             }
          },
          "2023-02-01":{
             "CHFEUR":{
                "close":1.00156,
                "high":1.00507,
                "low":0.9997,
                "open":1.00493
             },
             "CHFUSD":{
                "close":1.10089,
                "high":1.10213,
                "low":1.09005,
                "open":1.09204
             },
             "EURUSD":{
                "close":1.09892,
                "high":1.10013,
                "low":1.08525,
                "open":1.08637
             }
          },
          "2023-02-02":{
             "CHFEUR":{
                "close":1.0037,
                "high":1.00633,
                "low":0.99968,
                "open":1.00113
             },
             "CHFUSD":{
                "close":1.09513,
                "high":1.10353,
                "low":1.09439,
                "open":1.1
             },
             "EURUSD":{
                "close":1.0911,
                "high":1.10332,
                "low":1.08855,
                "open":1.09893
             }
          }
       },
       "start_date":"2023-01-30-00:00"
    }

这个想法是拉一个 Dataframe :
| 日期|瑞士法郎|瑞士法郎美元|欧元美元|
| - ------|- ------|- ------|- ------|
| 2023年1月30日|0.99925|一点零八五七九|一○八六零九|
| 2023年1月31日|0.99684|一点零八一二七|一点零八四九八|
| 二○二三年二月一日|一○ ○四九三|一○九二零四|一点零八六三七|
| 二○二三年二月二日|一○ ○一一三|1.1节|一○九八九三|
我尝试使用转置,但在此之后,我无法从每个值的内容中拉出值"打开":
x一个一个一个一个x一个一个二个x
关于如何在嵌套的json内容中只提取值"open",你有什么想法吗?
谢谢大家!

p8h8hvxi

p8h8hvxi1#

如果你只想用Pandas;-)

pd.DataFrame(data['price']).stack().str.get('open').unstack(0)
CHFEUR   CHFUSD   EURUSD
2023-01-30  0.99925  1.08579  1.08609
2023-01-31  0.99684  1.08127  1.08498
2023-02-01  1.00493  1.09204  1.08637
2023-02-02  1.00113  1.10000  1.09893
hgqdbh6s

hgqdbh6s2#

尝试(data包含问题中的字典):

all_data = []
for k, v in data['price'].items():
    all_data.append({'DATE':k, **{kk:v[kk]['open'] for kk in v}})

df = pd.DataFrame(all_data)
print(df)

图纸:

DATE   CHFEUR   CHFUSD   EURUSD
0  2023-01-30  0.99925  1.08579  1.08609
1  2023-01-31  0.99684  1.08127  1.08498
2  2023-02-01  1.00493  1.09204  1.08637
3  2023-02-02  1.00113  1.10000  1.09893
9bfwbjaz

9bfwbjaz3#

进口Pandas当PD
lst_data =[{'日期':键,'瑞士法郎':物料['瑞士法郎']['未结'],'瑞士法郎美元':物料['瑞士法郎美元']['未结'],'欧元美元':物料['欧元美元']['未结']}对于键,物料在数据中['价格']. items()]
df = pd. Dataframe (lst_data)
打印(df)

相关问题