【问题标题】:Slicing using the Index in Pandas在 Pandas 中使用索引进行切片
【发布时间】:2018-12-11 14:41:16
【问题描述】:

我正在尝试对 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

【问题讨论】:

    标签: python-3.x pandas indexing slice


    【解决方案1】:

    我认为需要 DataFrame.loc,否则 pandas 正在寻找列名 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
    

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 2014-10-21
      • 2014-05-02
      • 2021-07-18
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2012-09-17
      相关资源
      最近更新 更多