从Pandas MultiIndex数据帧中提取数据

时间:2017-02-09 10:51:24

标签: python pandas multi-index

在搜索数小时后,我仍然无法从multiindex数据帧中将一些数据提取到新的数据帧中。如果我完全诚实的话,理解这整个多索引的东西都有很大的问题:/

数据来自经合组织,使用pandas_datareader.data作为网络。

我正在搜索如何在特定时间段内获取特定国家/地区的特定数据。

有人可以帮助我吗?

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)

真的非常感谢你!

问候,大卫

1 个答案:

答案 0 :(得分:1)

你需要:

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
...
...

但不幸的是没有数据:

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