使用apply& lambda for serie

时间:2016-10-29 03:06:17

标签: python pandas

我有这个:

df.loc['United Kingdom']

这是一个系列:

Rank                        4.000000e+00
Documents                   2.094400e+04
Citable documents           2.035700e+04
Citations                   2.060910e+05
Self-citations              3.787400e+04
Citations per document      9.840000e+00
H index                     1.390000e+02
Energy Supply                        NaN
Energy Supply per Capita             NaN
% Renewable's                        NaN
2006                        2.419631e+12
2007                        2.482203e+12
2008                        2.470614e+12
2009                        2.367048e+12
2010                        2.403504e+12
2011                        2.450911e+12
2012                        2.479809e+12
2013                        2.533370e+12
2014                        2.605643e+12
2015                        2.666333e+12
Name: United Kingdom, dtype: float64

现在,我想要

  

apply(lambda x:x ['2015'] - x ['2006'])

但是它返回了一个错误:

  

TypeError:'float'对象不可订阅

但如果我把它分开:

  

df.loc ['United Kingdom'] ['2015'] - df.loc ['United Kingdom'] ['2006']

没关系。

我怎么能在这里申请和lambda?

感谢。

Ps:我想将它应用于Dataframe

Rank    Documents   Citable documents   Citations   Self-citations  Citations per document  H index Energy Supply   Energy Supply per Capita    % Renewable's   ... 2008    2009    2010    2011    2012    2013    2014    2015    Citation Ratio  Population
Country                                                                                 
China   1   127050  126767  597237  411683  4.70    138 NaN NaN NaN ... 4.997775e+12    5.459247e+12    6.039659e+12    6.612490e+12    7.124978e+12    7.672448e+12    8.230121e+12    8.797999e+12    0.689313    NaN
United States   2   96661   94747   792274  265436  8.20    230 NaN NaN NaN ... 1.501149e+13    1.459484e+13    1.496437e+13    1.520402e+13    1.554216e+13    1.577367e+13    1.615662e+13    1.654857e+13    0.335031    NaN
Japan   3   30504   30287   223024  61554   7.31    134 NaN NaN NaN ... 5.558527e+12    5.251308e+12    5.498718e+12    5.473738e+12    5.569102e+12    5.644659e+12    5.642884e+12    5.669563e+12    0.275997    NaN
United Kingdom  4   20944   20357   206091  37874   9.84    139 NaN NaN NaN ... 2.470614e+12    2.367048e+12    2.403504e+12    2.450911e+12    2.479809e+12    2.533370e+12    2.605643e+12    2.666333e+12    0.183773    NaN
enter code here

1 个答案:

答案 0 :(得分:0)

如果您想对所有数据框应用它,那么只需计算它:

df['2015'] - df['2006']