`head(10)`

1. 求最大或最小的前N组数据

2. 求当前元素和前一元素间变化率

3. 将列表中每个元素转化为一行

## 1. 求最大或最小的前N组数据

。一般情况下，我们可能会采用 `df.sort_values(columns, ascending=False).head(n)`

``DataFrame.nlargest(n,columns,keep='first')` `Series.nlargest(n=5,keep='first')``

keep参数可选值： first，可选 last 和 all （字面意思）

``````>>> import pandas as pd
>>> df = pd.DataFrame({'population': [59000000, 65000000, 434000,
...                                   434000, 434000, 337000, 11300,
...                                   11300, 11300],
...                    'GDP': [1937894, 2583560 , 12011, 4520, 12128,
...                            17036, 182, 38, 311],
...                    'alpha-2': ["IT", "FR", "MT", "MV", "BN",
...                                "IS", "NR", "TV", "AI"]},
...                   index=["Italy", "France", "Malta",
...                          "Maldives", "Brunei", "Iceland",
...                          "Nauru", "Tuvalu", "Anguilla"])
>>> df
population      GDP alpha-2
Italy       59000000  1937894      IT
France      65000000  2583560      FR
Malta         434000    12011      MT
Maldives      434000     4520      MV
Brunei        434000    12128      BN
Iceland       337000    17036      IS
Nauru          11300      182      NR
Tuvalu         11300       38      TV
Anguilla       11300      311      AI
``````

，实际有2个满足要求的数据行被我们漏掉了；这个时候使用 `df.nlargest(3, 'population',keep='all')`
，即可获取我们需要的结果。

``````>>> df.head(3)
population      GDP alpha-2
Italy     59000000  1937894      IT
France    65000000  2583560      FR
Malta       434000    12011      MT
>>> df.nlargest(3, 'population')
population      GDP alpha-2
France    65000000  2583560      FR
Italy     59000000  1937894      IT
Malta       434000    12011      MT
# keep = 'all' 即表示满足排名的全部返回
>>> df.nlargest(3, 'population',keep='all')
population      GDP alpha-2
France      65000000  2583560      FR
Italy       59000000  1937894      IT
Malta         434000    12011      MT
Maldives      434000     4520      MV
Brunei        434000    12128      BN
``````

``````>>> df.nlargest(3, ['population', 'GDP'])
population      GDP alpha-2
France    65000000  2583560      FR
Italy     59000000  1937894      IT
Brunei      434000    12128      BN
``````

``DataFrame.nsmallest(n,columns,keep='first')` `Series.nsmallest(n=5,keep='first')``

## 2. 求当前元素和前一元素间变化率

`pct_change(periods=1,fill_method='pad',limit=None,freq=None,kwargs)`

``````>>> s = pd.Series([90, 91, 85])
>>> s.pct_change()
0         NaN
1    0.011111
2   -0.065934
dtype: float64
# 设置间隔periods=2，在这里就是85相比90的变化率
>>> s.pct_change(periods=2)
0         NaN
1         NaN
2   -0.055556
dtype: float64
``````

``````>>> s = pd.Series([90, 91, None, 85])
>>> s
0    90.0
1    91.0
2     NaN
3    85.0
dtype: float64
>>> s.pct_change(fill_method='bfill')
0         NaN
1    0.011111
2   -0.065934
3    0.000000
dtype: float64
>>> s.pct_change(fill_method='ffill')
0         NaN
1    0.011111
2    0.000000
3   -0.065934
dtype: float64
>>> s.pct_change()
0         NaN
1    0.011111
2    0.000000
3   -0.065934
dtype: float64
``````

``````>>> df = pd.DataFrame({
...     'FR': [4.0405, 4.0963, 4.3149],
...     'GR': [1.7246, 1.7482, 1.8519],
...     'IT': [804.74, 810.01, 860.13]},
...     index=['1980-01-01', '1980-02-01', '1980-03-01'])
>>> df
FR      GR      IT
1980-01-01  4.0405  1.7246  804.74
1980-02-01  4.0963  1.7482  810.01
1980-03-01  4.3149  1.8519  860.13
>>> df.pct_change()
FR        GR        IT
1980-01-01       NaN       NaN       NaN
1980-02-01  0.013810  0.013684  0.006549
1980-03-01  0.053365  0.059318  0.061876
>>> df = pd.DataFrame({
...     '2016': [1769950, 30586265],
...     '2015': [1500923, 40912316],
...     '2014': [1371819, 41403351]},
...     index=['GOOG', 'APPL'])
>>> df
2016      2015      2014
GOOG   1769950   1500923   1371819
APPL  30586265  40912316  41403351
# 对行进行操作axis=1或'columns'
>>> df.pct_change(axis = 1)
2016      2015      2014
GOOG   NaN -0.151997 -0.086016
APPL   NaN  0.337604  0.012002
>>> df.pct_change(axis = 'columns')
2016      2015      2014
GOOG   NaN -0.151997 -0.086016
APPL   NaN  0.337604  0.012002
``````

## 3. 将列表中每个元素转化为一行

``Series.explode(ignore_index=False)` `DataFrame.explode(column,ignore_index=False)``

``````>>> s = pd.Series([[1, 2, 3], 'foo', [], [3, 4]])
>>> s
0    [1, 2, 3]
1          foo
2           []
3       [3, 4]
dtype: object
# 默认情况下，会复制索引
>>> s.explode()
0      1
0      2
0      3
1    foo
2    NaN
3      3
3      4
dtype: object
# 设置参数ignore_index=True，则会重置索引
>>> s.explode(ignore_index=True)
0      1
1      2
2      3
3    foo
4    NaN
5      3
6      4
dtype: object
``````

``````>>> df = pd.DataFrame({'A': [[1, 2, 3], 'foo', [], [3, 4]], 'B': 1})
>>> df
A  B
0  [1, 2, 3]  1
1        foo  1
2         []  1
3     [3, 4]  1
# 默认情况下，会复制索引
>>> df.explode('A')
A  B
0    1  1
0    2  1
0    3  1
1  foo  1
2  NaN  1
3    3  1
3    4  1
# 设置参数ignore_index=True，则会重置索引
>>> df.explode('A',ignore_index=True)
A  B
0    1  1
1    2  1
2    3  1
3  foo  1
4  NaN  1
5    3  1
6    4  1
``````