2

用 PANDAS 或 Plotly 绘制 SDMX 数据的最简单方法是什么?

我有以下代码:

import pandasdmx as sdmx 
import plotly.express as px

df = sdmx.Request('OECD').data(
  resource_id='MEI_FIN',
  key='IR3TIB.GBR+USA.M',
  params={'startTime': '1900-06', 'dimensionAtObservation': 'TimeDimension'},
).write().reset_index()
df

我在尝试绘图时最终收到以下错误

fig = px.line(df, x="TIME_PERIOD", y='', title='Life expectancy in Country: Denmark')
fig.show()

如下:

ValueError: Value of 'y' is not the name of a column in 'data_frame'. Expected one of `[('TIME_PERIOD', '', ''), ('IR3TIB', 'GBR', 'M'), ('IR3TIB', 'USA', 'M')] but received:` 

我对 python 很陌生,所以我会感谢每一个可以帮助我的评论。

4

1 回答 1

0

我认为您的主要问题是由于您的 df 使用多索引。我不确定这是否是您要实现的目标,但您可以尝试以下代码:

import pandasdmx as sdmx 
import plotly.express as px

df = sdmx.Request('OECD').data(
  resource_id='MEI_FIN',
  key='IR3TIB.GBR+USA.M',
  params={'startTime': '1900-06', 'dimensionAtObservation': 'TimeDimension'},
).write().reset_index()

# with this we get rid of multi-index
# you could use a loop if you prefer I used
# list of comprehension

df.columns = ["_".join([c for c in col if c!='']) 
              for col in df.columns]

fig = px.line(df,
              x="TIME_PERIOD",
              y=['IR3TIB_GBR_M', 'IR3TIB_USA_M'],
              title='Life expectancy in GBR and USA')\
        .update_layout(title_x=0.5)
fig.show()

在此处输入图像描述

于 2020-07-08T15:26:04.907 回答