简体   繁体   中英

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

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: []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM