Pandas and Numpy common method summary

Lambda functions to achieve

Simply put, lambda is a function, but this function has no name, so call us about the form of this function, parameter and return value realization. lambda following format:

lambda [arg1 [, agr2,.....argn]] : expression
lambda x : expression

So how this function is used, it is often not used alone, when used alone may be relatively simple, too simple to achieve. So normally used is that the parameters of a function is a function, then this parameter can be achieved using lambda.

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>> list(map(lambda x: x * 2 + 10, foo)) 
# 这里的 map 函数的第一个参数就是函数

Apply a function of Pandas

apply function as follows DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds). Its core is the function of choice, followed by axis represents a dimension of this function by the above said lambda function implementation. This parameter function is DataFrame, the returned object is either DataFrame can also be a series.

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B'])
>>> df.apply(np.sqrt)
     A    B
0  2.0  3.0
1  2.0  3.0
2  2.0  3.0
# 返回的是一个 DataFrame

>>> df.apply(np.sum, axis=0)
A    12
B    27
dtype: int64 # 返回的是一个 Series
>>> df.apply(lambda x: x*2 + 1, axis = 1) # 这种情况下,x 表示的是 df 中所有的参数
   A   B
0  9  19
1  9  19
2  9  19
>>> df.apply(lambda x: [1, 2,5], axis=1)
0    [1, 2, 5]
1    [1, 2, 5]
2    [1, 2, 5]
>>> df.apply(lambda x: [1, 2,5], axis=0)
   A  B
0  1  1
1  2  2
2  5  5
>>> df.apply(lambda x: [1, 2,6,7,8,5], axis=0)
A    [1, 2, 6, 7, 8, 5]
B    [1, 2, 6, 7, 8, 5]
>>> type(df.apply(lambda x: [1, 2,6,7,8,5], axis=0))
<class 'pandas.core.series.Series'>
# 这时,将DataFrame变成一个 Series。

python Zip Function

Use zip function is zip([iterable, …]). zip () function is a built-in Python, which can be subjected to a series of iterations the object as a parameter, the corresponding element of the object packed into one tuple (tuples), and then returns the list (list) from these tuples thereof.

>>> name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]
>>> roll_no = [ 4, 1, 3, 2 ]
>>> marks = [ 40, 50, 60, 70 ]
>>> mapped = zip(name, roll_no, marks)
>>> list(mapped)
[('Manjeet', 4, 40), ('Nikhil', 1, 50), ('Shambhavi', 3, 60), ('Astha', 2, 70)]

Pandas in the Map function

Map function is mainly DataFrame operation, which may also be a function parameter,

>>> import pandas as pd
>>> from pandas import Series, DataFrame
>>> data = DataFrame({'food':['bacon','pulled pork','bacon','Pastrami',
   'corned beef','Bacon','pastrami','honey ham','nova lox'],
     'ounces':[4,3,12,6,7.5,8,3,5,6]})
>>> data
          food  ounces
0        bacon     4.0
1  pulled pork     3.0
2        bacon    12.0
3     Pastrami     6.0
4  corned beef     7.5
5        Bacon     8.0
6     pastrami     3.0
7    honey ham     5.0
8     nova lox     6.0
>>> meat_to_animal = {
 'bacon':'pig',
 'pulled pork':'pig',
 'pastrami':'cow',
 'corned beef':'cow',
 'honey ham':'pig',
 'nova lox':'salmon' } 

>>> meat_to_animal
{'bacon': 'pig', 'pulled pork': 'pig', 'pastrami': 'cow', 'corned beef': 'cow', 'honey ham': 'pig', 'nova lox': 'salmon'}
>>> data['food'].map(str.lower)
0          bacon
1    pulled pork
2          bacon
3       pastrami
4    corned beef
5          bacon
6       pastrami
7      honey ham
8       nova lox
Name: food, dtype: object
>>> data['animal'] = data['food'].map(str.lower).map(meat_to_animal)
>>> data
          food  ounces  animal
0        bacon     4.0     pig
1  pulled pork     3.0     pig
2        bacon    12.0     pig
3     Pastrami     6.0     cow
4  corned beef     7.5     cow
5        Bacon     8.0     pig
6     pastrami     3.0     cow
7    honey ham     5.0     pig
8     nova lox     6.0  salmon
>>> data['ounces'] = data['ounces'].map(lambda x: x+ 2) # 这里使用 Map 函数与Apply函数有点类似
>>> data
          food  ounces  animal
0        bacon     6.0     pig
1  pulled pork     5.0     pig
2        bacon    14.0     pig
3     Pastrami     8.0     cow
4  corned beef     9.5     cow
5        Bacon    10.0     pig
6     pastrami     5.0     cow
7    honey ham     7.0     pig
8     nova lox     8.0  salmon

Numpy the stack (), hstack (), vstack () function

stack () function

Function prototype: stack (arrays, axis = 0), arrays can be transferred arrays and lists. axis meaning I will explain below, let's look at an example.

>>> import numpy as np
>>> a=[[[1,2,3,4],[11,21,31,41]],
   [[5,6,7,8],[51,61,71,81]],
   [[9,10,11,12],[91,101,111,121]]]
>>> a
[[[1, 2, 3, 4], [11, 21, 31, 41]], [[5, 6, 7, 8], [51, 61, 71, 81]], [[9, 10, 11, 12], [91, 101, 111, 121]]]
# 可以看成 a 有三层,我们把从外到里分别看成 axis = 0, axis = 1, axis = 2的三层,首先要确定这个 list a,有三个元素,每个元素都# 是一个 list_1,每个 lsit_1 有两个 list_2 元素, 
>>> np.stack(a, axis = 0)
array([[[  1,   2,   3,   4],
        [ 11,  21,  31,  41]],

       [[  5,   6,   7,   8],
        [ 51,  61,  71,  81]],

       [[  9,  10,  11,  12],
        [ 91, 101, 111, 121]]])
>>> d = np.stack(a, axis = 0)
>>> len(d)
3
>>> d.shape         # 在shape中分别表示从外到里的维度
(3, 2, 4)
# 得到的是一个 array 的类型,堆叠的是 axis = 0的那一层,相当于没变,只是数据格式改变
>>> np.stack(a, axis = 1)
array([[[  1,   2,   3,   4],
        [  5,   6,   7,   8],
        [  9,  10,  11,  12]],

       [[ 11,  21,  31,  41],
        [ 51,  61,  71,  81],
        [ 91, 101, 111, 121]]])
>>> c = np.stack(a, axis = 1)
>>> c.shape
(2, 3, 4)
# 这里获取 array 的每个元素的方式
>>> np.stack(a, axis = 2)
array([[[  1,   5,   9],
        [  2,   6,  10],
        [  3,   7,  11],
        [  4,   8,  12]],

       [[ 11,  51,  91],
        [ 21,  61, 101],
        [ 31,  71, 111],
        [ 41,  81, 121]]])
>>> b = np.stack(a, axis = 2)
>>> b.shape
(2, 4, 3)

We can understand, stack the course of that layer stacked elements, these elements as most new Array inner layer, axis! = 0 when the elements are always the first layer stack into a new innermost element.

hstack () function

For the example above, we have to be converted on a good understanding hstack () function of the

>>> d = np.stack(a, axis = -1)
>>> d
array([[[  1,   5,   9],
        [  2,   6,  10],
        [  3,   7,  11],
        [  4,   8,  12]],

       [[ 11,  51,  91],
        [ 21,  61, 101],
        [ 31,  71, 111],
        [ 41,  81, 121]]])
>>> d = np.hstack(d)
>>> d
array([[  1,   5,   9,  11,  51,  91],
       [  2,   6,  10,  21,  61, 101],
       [  3,   7,  11,  31,  71, 111],
       [  4,   8,  12,  41,  81, 121]])
>>> d = np.hstack(d)
>>> d
array([  1,   5,   9,  11,  51,  91,   2,   6,  10,  21,  61, 101,   3,
         7,  11,  31,  71, 111,   4,   8,  12,  41,  81, 121])
>>> a = [[[[1, 2, 3, 4], [11, 21, 31, 41]], [[5, 6, 7, 8], [51, 61, 71, 81]], [[9, 10, 11, 12], [91, 101, 111, 121]]]]
>>> a
[[[[1, 2, 3, 4], [11, 21, 31, 41]], [[5, 6, 7, 8], [51, 61, 71, 81]], [[9, 10, 11, 12], [91, 101, 111, 121]]]]
 hstack() 还可以用于两个array 的横向合并
>>> a=[[1],[2],[3]]
>>> b=[[1],[2],[3]]
>>> np.hstack((a,b))
array([[1, 1],
       [2, 2],
       [3, 3]])
 vstack() 函数用于列的合并,也就是纵向
>>> np.vstack((a,b))
array([[1],
       [2],
       [3],
       [1],
       [2],
       [3]])

Guess you like

Origin www.cnblogs.com/wevolf/p/10991789.html