DavidV
DavidV

Reputation: 533

Extracting data from Pandas MultiIndex dataframe

After hours spent searching I still cannot extract some data into new dataframe from multiindex dataframe. If I am totally honest I have big problems understanding this whole multiindex stufff :/

Data is from OECD, using pandas_datareader.data as web.

I am searching how to get specific data for specific country on specific time period.

Could someone help me?

import pandas_datareader.data as web
import pandas as pd

podatki = web.DataReader('MEI_CLI', data_source='oecd')

df = pd.DataFrame(podatki)

#Below is the data that I'm searching.
#does not work!!!

df = df.xs('Original, seasonally adjusted (GDP)','Slovenia','Annual') 
print(df)

Really really thank you!

Regards, David

Upvotes: 1

Views: 601

Answers (1)

jezrael
jezrael

Reputation: 862406

You need:

podatki = web.DataReader('MEI_CLI', data_source='oecd')
print (podatki)

df = podatki.xs(('Original, seasonally adjusted (GDP)', 'Slovenia', 'Annual'), 
                 level=('Subject', 'Country','Frequency'), axis=1) 

print(df)
Subject   Original, seasonally adjusted (GDP)
Country                              Slovenia
Frequency                              Annual
Time                                         
1990                                      NaN
1991                                      NaN
1992                                      NaN
1993                                      NaN
1994                                      NaN
1995                                      NaN
1996                                      NaN
1997                                      NaN
1998                                      NaN
1999                                      NaN
2000                                      NaN
2001                                      NaN
2002                                      NaN
...
...

But unfortunately there are no data:

print(df.dropna())
Empty DataFrame
Columns: [(Original, seasonally adjusted (GDP), Slovenia, Annual)]
Index: []

Upvotes: 1

Related Questions