在熊猫中使用索引切片

时间:2018-07-03 10:10:40

标签: python-3.x pandas indexing slice

我正在尝试对与2010年相对应的值进行切片,但收到一条我无法解释的错误消息。

df1

    GDP USA_GDP_Deflator
Year        
2005    14408093840400  90.877573
2006    14792303791800  93.669574
2007    15055395304800  96.162437
2008    15011490541400  98.048771
2009    14594842181900  98.793388
2010    14964372000000  100.000000
2011    15204019634600  102.064628
2012    15542161722300  103.944710
2013    15802855301300  105.623425
2014    16208861247400  107.519021

df1.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 2005 to 2014
Data columns (total 2 columns):
GDP                 10 non-null int64
USA_GDP_Deflator    10 non-null float64
dtypes: float64(1), int64(1)
memory usage: 240.0 bytes

df1[2010]
KeyError: 2010

您的建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为需要DataFrame.loc,否则大熊猫正在寻找列名2010,并且由于不存在而引发错误:

df1.loc[2010]

#rename column for 2010 column
df1 = df1.rename(columns={'USA_GDP_Deflator':2010})
print (df1)
                 GDP        2010
Year                            
2005  14408093840400   90.877573
2006  14792303791800   93.669574
2007  15055395304800   96.162437
2008  15011490541400   98.048771
2009  14594842181900   98.793388
2010  14964372000000  100.000000
2011  15204019634600  102.064628
2012  15542161722300  103.944710
2013  15802855301300  105.623425
2014  16208861247400  107.519021

#selected column 2010
print(df1[2010])
Year
2005     90.877573
2006     93.669574
2007     96.162437
2008     98.048771
2009     98.793388
2010    100.000000
2011    102.064628
2012    103.944710
2013    105.623425
2014    107.519021
Name: 2010, dtype: float64

#selected row 2010
print(df1.loc[2010])
GDP     1.496437e+13
2010    1.000000e+02
Name: 2010, dtype: float64