Learn these useful pandas functions to make your data processing faster

If you want to get the information of the top 10 students in the total score, you may sort them according to the total score and then head(10)do some operations, but what if the number of students in the top 10 is more than 10 swollen when you encounter the same ranking?

Today, let's take a look at the convenient function methods provided by pandas to make our data processing one step faster~

content:

1. Find the largest or smallest top N groups of data

When we process data, we often encounter a scenario, that is, to find the largest or smallest top N groups of data in this group of data . Under normal circumstances, we may use df.sort_values(columns, ascending=False).head(n)to obtain, but there are often some data ranked in parallel that will be ruthlessly truncated and cannot be obtained. So, today we can try the following methods, and it will be fine.

Let's take the example of finding the largest first N groups of data as an example:

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

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

The optional value of the keep parameter: the default is first, optional last and all (literally)

We first construct a case data

>>> 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
复制代码

For the above case data, if we want to get the top 3 sets of data with the most population fields, we find that the third place is 434000. If it is adopted head(3), there are actually 2 data rows that meet the requirements are missed by us; at this time df.nlargest(3, 'population',keep='all'), we can get the results we need.

>>> 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
复制代码

Of course, we may face more complex requirements, such as taking the largest top N groups of data according to multiple fields. In this case, we need to take the top three data in GDP with the most population.

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

For the smallest first N groups of data, the function is as follows (parameters have the same meaning):

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

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

2. 求当前元素和前一元素之间百分比变化

有时候,我们的数据可能是时间序列下的,为了更方便看到随着时间变化某行或列数据的变化率,这里就可以采用pct_change方法直接获取。

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

先看看对于Series类型数据:

>>> 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
复制代码

对于有缺失值的情况,我们可以填充缺失值后参与计算或者在计算百分比时设置填充参数fill_method

>>> 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
复制代码

也可以直接对Dataframe类型数据进行处理:

>>> 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. 将列表中每个元素转化为一行

有时候,我们的原始数据中某些元素可能是列表的形式,而我们需要对它进行展开操作,于是explode方法就来了。

Series.explode( ignore_index=False)

DataFrame.explode( column, ignore_index=False)

先看看对Series类型数据的处理:

>>> 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
复制代码

再看看在Dataframe类型数据下的操作:

>>> 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
复制代码

Guess you like

Origin juejin.im/post/6945117512698855438