莫烦Python Numpy&Pandas 学习笔记

莫烦Python Numpy&Pandas 学习笔记

原文(视频)地址:https://www.bilibili.com/video/BV1Ex411L7oT

1. 安装

numpy官方网站:download numpy

#pip3 install numpy pandas

2. numpy基本属性

# 导入
import numpy as np
# 定义一个矩阵
array = np.array([[1, 2, 3], [2, 3, 4]])
print(array)
[[1 2 3]
 [2 3 4]]
print("number of dim:", array.ndim)  # 维度
print("shape:", array.shape)  # 形状
print("size:", array.size)  # 元素个数
number of dim: 2
shape: (2, 3)
size: 6

3. 创建array

import numpy as np
a= np.array([2, 23, 4], dtype=np.int64)  # 对类型做定义
print(a)
print(a.dtype)
[ 2 23  4]
int64
# 矩阵
a = np.array([[2, 34, 4],
             [2, 32, 4]])
print(a)
[[ 2 34  4]
 [ 2 32  4]]
# 0矩阵
a = np.zeros((3, 4))
print(a)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
# 1矩阵
a = np.ones((3, 4), dtype=np.float)
print(a)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
# empty矩阵
b = np.empty((3, 4))
print(b)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
# 有序矩阵
b = np.arange(10, 20, 2)  # 从10到20,步长为2
print(b)
[10 12 14 16 18]
# reshape改变形状
b = np.arange(12).reshape((3, 4))
print(b)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
# 线段
a = np.linspace(1, 10, 6).reshape((2, 3))  # 从1到10,要6个数,形状为2行3列
print(a)
[[ 1.   2.8  4.6]
 [ 6.4  8.2 10. ]]

4. 基础运算

import numpy as np
a = np.array([10, 20, 30, 40])
b = np.arange(4)
c = a - b  # 同样支持 + * **
print(a, b, c)
[10 20 30 40] [0 1 2 3] [10 19 28 37]
c = 10*np.sin(a)  # 三角函数
print(c)
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
print(b)
print(b<3)  # 比较
[0 1 2 3]
[ True  True  True False]
a = np.array([[1, 1], [0, 1]])
b = np.arange(4).reshape((2,2))
print(a)
print(b)
[[1 1]
 [0 1]]
[[0 1]
 [2 3]]
print(a*b)  # 对应位置相乘
print(a.dot(b))  # 矩阵乘法
print(np.dot(a, b))  # 矩阵乘法另一种写法
[[0 1]
 [0 3]]
[[2 4]
 [2 3]]
[[2 4]
 [2 3]]
a = np.random.random((2, 4))  # 随机[0,1),形状为2*4
print(a)
[[0.8410904  0.60731039 0.2572012  0.42907328]
 [0.92080189 0.10937016 0.70279503 0.94849117]]
print(np.sum(a))
print(np.min(a))
print(np.max(a))
4.816133528743711
0.10937015671859662
0.9484911685139872
print(np.sum(a, axis=1)) # axis=1,对列作操作,相当于每一行求和
print(np.max(a, axis=0)) # axis=0,对行做操作,相当于每一列中求最大
[2.13467528 2.68145825]
[0.92080189 0.60731039 0.70279503 0.94849117]
A = np.arange(2, 14).reshape((3, 4))
print(np.argmin(A))  # 最小值索引
print(np.argmax(A))  # 最大值索引
0
11
print("A: ", A)
print("np.mean(A): ", np.mean(A))  # 平均值,可指定axis=0每列的平均,axis=1每行的平均
print("np.mean(A, axis=0): ", np.mean(A, axis=0))  # axis=0对行作操作,相当于每一列
print("np.mean(A, axis=1): ", np.mean(A, axis=1))  # axis=1对列作操作,相当于每一行
print("np.average(A): ", np.average(A))  # 平均值
print("np.median(A): ", np.median(A))  # 中位数
print("np.cumsum(A): ", np.cumsum(A))  # 累加
print("np.diff(A): ", np.diff(A)) # 累差
print("np.nonzero(A): ", np.nonzero(A))  # 非零元素的位置,第一个数组为行,第二个数组为列,一对一。
print("np.sort(A): ", np.sort(A))  # 排序.
print("np.transpose(A): ", np.transpose(A))  # 反向,转置
print("A.T: ", A.T)  # 转置
print("np.clip(A, 5, 9, ): ", np.clip(A, 5, 9, ))  # <5的成为5,>9的成为9
A:  [[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
np.mean(A):  7.5
np.mean(A, axis=0):  [6. 7. 8. 9.]
np.mean(A, axis=1):  [ 3.5  7.5 11.5]
np.average(A):  7.5
np.median(A):  7.5
np.cumsum(A):  [ 2  5  9 14 20 27 35 44 54 65 77 90]
np.diff(A):  [[1 1 1]
 [1 1 1]
 [1 1 1]]
np.nonzero(A):  (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
np.sort(A):  [[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
np.transpose(A):  [[ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]
 [ 5  9 13]]
A.T:  [[ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]
 [ 5  9 13]]
np.clip(A, 5, 9, ):  [[5 5 5 5]
 [6 7 8 9]
 [9 9 9 9]]

5. numpy的索引

A = np.arange(3, 15)
print(A)
[ 3  4  5  6  7  8  9 10 11 12 13 14]
print(A[3]) # 打印索引为3的元素的值
6
A = A.reshape((3, 4))  # 改变形状3*4
print("A:", A)
print("A[2]:", A[2])  # 打印索引为2的行
A: [[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
A[2]: [11 12 13 14]
print(A[1][1]) # 第二行第二列,不推荐使用这种方式进行索引
8
print(A[1, 1])  # 建议使用这种方式
8
print(A[2, :])  # 第三行所有数
[11 12 13 14]
print(A[1, 1:3])  # 做切片
[8 9]
for row in A:  # 打印每一行
    print(row)
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
print(A.T)
for column in A.T:  # 打印每一列
    print(column)
[[ 3  7 11]
 [ 4  8 12]
 [ 5  9 13]
 [ 6 10 14]]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
print(A.flatten())
for item in A.flat:  # 打印每一项,A.flat为迭代器
    print(item)
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14

6. 合并

print("out:")
A = np.array([1,1,1])
B = np.array([2,2,2])
print(np.vstack((A, B)))  # 垂直方向上合并
out:
[[1 1 1]
 [2 2 2]]
print("out:")
C = np.vstack((A, B))
print(C.shape)  # 合并后的shape
out:
(2, 3)
print("out:")
D = np.hstack((A, B))  # 水平方向上合并
print(D)
print(D.shape)  # 水平方向合并后的shape
out:
[1 1 1 2 2 2]
(6,)
print("out:")
print(A.T)  # 一个维度,横向变竖向:失败
out:
[1 1 1]
print("out:")
print(A[np.newaxis, :])  # 多加一个维度
print(A[:, np.newaxis])
out:
[[1 1 1]]
[[1]
 [1]
 [1]]
print("out:")
print(np.hstack((A[:, np.newaxis], B[:, np.newaxis])))
out:
[[1 2]
 [1 2]
 [1 2]]
print("out:")
A = A[:, np.newaxis]
B = B[:, np.newaxis]
C = np.concatenate((A, B, B, A), axis=0)  # 多个array合并
print(C)
out:
[[1]
 [1]
 [1]
 [2]
 [2]
 [2]
 [2]
 [2]
 [2]
 [1]
 [1]
 [1]]

7. 分割

A = np.arange(12).reshape((3, 4))
print(A)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
print(np.split(A, 2, axis=1))
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
# print(np.split(A, 3, axis=1))  # 不能进行不等的分割
print(np.array_split(A, 3, axis=1))  # 进行不等量的分割
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
print(np.vsplit(A, 3))
print(np.hsplit(A, 2))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]

8. 浅拷贝&深拷贝

a = np.arange(4)
print(a)
[0 1 2 3]
b = a
c = a
d = b  # 全是浅拷贝,共用同一块存储空间,改变任一个,其他的也改变
a[0] = 11
print(b)
print(d)
[11  1  2  3]
[11  1  2  3]
print(b is a)  # b是完全的a
print(d is a)  # d是完全的a
True
True
b = a.copy()  # 深拷贝,b和a指向不同的内存地址
print(b is a)
False
a[2] = 22
print(b)  # b没有被改变
[11  1  2  3]

pandas

1. 基本介绍

import pandas as pd
import numpy as np
s = pd.Series([1, 3, 6, np.nan, 44, 1])  # 一个Series
print(s)
0     1.0
1     3.0
2     6.0
3     NaN
4    44.0
5     1.0
dtype: float64
dates = pd.date_range('20160101', periods=6)  # 时间日期range
print(dates)
DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',
               '2016-01-05', '2016-01-06'],
              dtype='datetime64[ns]', freq='D')
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd'])  # 指定数据、索引和列名
print(df)
                   a         b         c         d
2016-01-01 -1.742936  1.751451 -1.369113  0.626934
2016-01-02  0.550725 -0.640860 -0.309800  1.268160
2016-01-03 -0.194772  1.615315  1.015855 -0.176221
2016-01-04 -0.339469 -1.418400  0.146489  0.957804
2016-01-05 -0.844710 -0.292351 -0.286376 -0.658872
2016-01-06  1.736696  1.144769 -0.178112 -1.267666
df1 = pd.DataFrame(np.random.randn(12).reshape((3, 4)))  # 不指定索引和列名
print(df1)
          0         1         2         3
0  0.453689 -0.691620  0.743595 -1.090077
1 -0.347534  1.358612 -0.187446  0.322624
2  1.309691  0.007173 -1.440739 -1.164850
# 键值对形式定义
df2 = pd.DataFrame({
     'A':1.,
                    'B':pd.Timestamp('20130102'), 
                    'C':pd.Series(1, index=list(range(4)), dtype='float32'), 
                    'D':np.array([3]*4,dtype='int32'),
                   'E':pd.Categorical(["test","train","test", "train"]),
                   'F':'foo'})
print(df2)
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo
print(df2.dtypes) # 列的类型
A           float64
B    datetime64[ns]
C           float32
D             int32
E          category
F            object
dtype: object
print(df2.index)  # 列的序号
Int64Index([0, 1, 2, 3], dtype='int64')
print(df2.columns)  # 列的名字
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
print(df2.values)  # 元素值
[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
df2.describe()  # 仅能描述数值形式的列
A C D
count 4.0 4.0 4.0
mean 1.0 1.0 3.0
std 0.0 0.0 0.0
min 1.0 1.0 3.0
25% 1.0 1.0 3.0
50% 1.0 1.0 3.0
75% 1.0 1.0 3.0
max 1.0 1.0 3.0
print(df2.T)  # 转置
                     0                    1                    2  \
A                    1                    1                    1   
B  2013-01-02 00:00:00  2013-01-02 00:00:00  2013-01-02 00:00:00   
C                    1                    1                    1   
D                    3                    3                    3   
E                 test                train                 test   
F                  foo                  foo                  foo   

                     3  
A                    1  
B  2013-01-02 00:00:00  
C                    1  
D                    3  
E                train  
F                  foo  
print(df2.sort_index(axis=1, ascending=False))  # 按列排序,逆序
     F      E  D    C          B    A
0  foo   test  3  1.0 2013-01-02  1.0
1  foo  train  3  1.0 2013-01-02  1.0
2  foo   test  3  1.0 2013-01-02  1.0
3  foo  train  3  1.0 2013-01-02  1.0
print(df2.sort_index(axis=0, ascending=False))  # 按行排序,逆序
     A          B    C  D      E    F
3  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
0  1.0 2013-01-02  1.0  3   test  foo
print(df2.sort_values(by='E'))  # 对值排序,选关键字
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
2  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
3  1.0 2013-01-02  1.0  3  train  foo

2. 选择数据

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6, 4)), index=dates, columns=['A', 'B', 'C', 'D'])
print(df)
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
print(df['A'])  # 选择列
print(df.A)  # 同上
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int32
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int32
print(df[0:3])  # 按行切片
print(df['20130102':'20130104'])  # 闭区间
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11
             A   B   C   D
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
print(df.loc['20130102'])  # 通过标签选择
A    4
B    5
C    6
D    7
Name: 2013-01-02 00:00:00, dtype: int32
print(df.loc[:, ['A', 'B']])
             A   B
2013-01-01   0   1
2013-01-02   4   5
2013-01-03   8   9
2013-01-04  12  13
2013-01-05  16  17
2013-01-06  20  21
print(df.loc['20130102', ['A', 'B']])
A    4
B    5
Name: 2013-01-02 00:00:00, dtype: int32
print(df.iloc[3])  # 通过position来选择
A    12
B    13
C    14
D    15
Name: 2013-01-04 00:00:00, dtype: int32
print(df.iloc[3, 1])
13
print(df.iloc[3:5, 1:3])  # 切片形式
             B   C
2013-01-04  13  14
2013-01-05  17  18
print(df.iloc[[1,3,5], 1:3])  # 不连续的切片
             B   C
2013-01-02   5   6
2013-01-04  13  14
2013-01-06  21  22
print(df.ix[:3, ['A', 'C']])  # 混合筛选
            A   C
2013-01-01  0   2
2013-01-02  4   6
2013-01-03  8  10


C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.
print(df)
print(df[df.A > 8])  # Boolean index打印在特征A上值大于8的所有行
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
             A   B   C   D
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23

3. 设置值

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6, 4)), index=dates,columns=['A', 'B', 'C', 'D'])
print(df)
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
df.iloc[2, 2] = 1111
print(df)
             A   B     C   D
2013-01-01   0   1     2   3
2013-01-02   4   5     6   7
2013-01-03   8   9  1111  11
2013-01-04  12  13    14  15
2013-01-05  16  17    18  19
2013-01-06  20  21    22  23
df.loc['20130101', 'B'] = 2222
print(df)
             A     B     C   D
2013-01-01   0  2222     2   3
2013-01-02   4     5     6   7
2013-01-03   8     9  1111  11
2013-01-04  12    13    14  15
2013-01-05  16    17    18  19
2013-01-06  20    21    22  23
df[df.A>4] = 0
print(df)
            A     B  C  D
2013-01-01  0  2222  2  3
2013-01-02  4     5  6  7
2013-01-03  0     0  0  0
2013-01-04  0     0  0  0
2013-01-05  0     0  0  0
2013-01-06  0     0  0  0
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6, 4)), index=dates,columns=['A', 'B', 'C', 'D'])
df.B[df.A > 4] = 0
print(df)
             A  B   C   D
2013-01-01   0  1   2   3
2013-01-02   4  5   6   7
2013-01-03   8  0  10  11
2013-01-04  12  0  14  15
2013-01-05  16  0  18  19
2013-01-06  20  0  22  23
df['F'] = np.nan
print(df)
             A  B   C   D   F
2013-01-01   0  1   2   3 NaN
2013-01-02   4  5   6   7 NaN
2013-01-03   8  0  10  11 NaN
2013-01-04  12  0  14  15 NaN
2013-01-05  16  0  18  19 NaN
2013-01-06  20  0  22  23 NaN
df['E'] = pd.Series([1, 2, 3, 4, 5,6], index=dates)
print(df)
             A  B   C   D   F  E
2013-01-01   0  1   2   3 NaN  1
2013-01-02   4  5   6   7 NaN  2
2013-01-03   8  0  10  11 NaN  3
2013-01-04  12  0  14  15 NaN  4
2013-01-05  16  0  18  19 NaN  5
2013-01-06  20  0  22  23 NaN  6

4. 处理丢失数据

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6, 4)), index=dates,columns=['A', 'B', 'C', 'D'])
df.iloc[0, 1] = np.nan
df.iloc[1, 2] = np.nan
print(df)
             A     B     C   D
2013-01-01   0   NaN   2.0   3
2013-01-02   4   5.0   NaN   7
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
print(df.dropna(axis=0, how='any'))  # how={'any', 'all'},丢弃那一行数据
             A     B     C   D
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
print(df.fillna(value=0))
             A     B     C   D
2013-01-01   0   0.0   2.0   3
2013-01-02   4   5.0   0.0   7
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
print(df.isnull())
                A      B      C      D
2013-01-01  False   True  False  False
2013-01-02  False  False   True  False
2013-01-03  False  False  False  False
2013-01-04  False  False  False  False
2013-01-05  False  False  False  False
2013-01-06  False  False  False  False
print(np.any(df.isnull()) == True)  # 判断是否有缺失的值
True

5. 导入导出数据

data = pd.read_csv('student.csv')  # 导入
print(data)  # 自动加了索引
   Student ID   name  age  gender
0        1100  Kelly   22  Female
1        1101    Tom   21  Female
2        1102  Tilly   23    Male
3        1103  David   20    Male
4        1104  Catty   22  Female
data.to_pickle('student.pickle')  # 导出为pandas的数据格式

6. 合并concatenating

df1 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd']) 
df2 = pd.DataFrame(np.ones((3, 4))*1, columns=['a', 'b', 'c', 'd']) 
df3 = pd.DataFrame(np.ones((3, 4))*2, columns=['a', 'b', 'c', 'd']) 
print(df1)
print(df2)
print(df3)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
     a    b    c    d
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
     a    b    c    d
0  2.0  2.0  2.0  2.0
1  2.0  2.0  2.0  2.0
2  2.0  2.0  2.0  2.0
res = pd.concat([df1, df2, df3], axis=0)  # 竖向合并
print(res)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
0  2.0  2.0  2.0  2.0
1  2.0  2.0  2.0  2.0
2  2.0  2.0  2.0  2.0
res = pd.concat([df1, df2, df3], axis=0, ignore_index=True)  # index重新进行排序
print(res)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
5  1.0  1.0  1.0  1.0
6  2.0  2.0  2.0  2.0
7  2.0  2.0  2.0  2.0
8  2.0  2.0  2.0  2.0
# join, ['inner', 'outer']
df1 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd'], index=[1, 2, 3]) 
df2 = pd.DataFrame(np.ones((3, 4))*1, columns=['b', 'c', 'd', 'e'], index=[2, 3, 4]) 
res = pd.concat([df1, df2], join='outer', sort=False)  # 所有特征都保留
print(res)
     a    b    c    d    e
1  0.0  0.0  0.0  0.0  NaN
2  0.0  0.0  0.0  0.0  NaN
3  0.0  0.0  0.0  0.0  NaN
2  NaN  1.0  1.0  1.0  1.0
3  NaN  1.0  1.0  1.0  1.0
4  NaN  1.0  1.0  1.0  1.0
res = pd.concat([df1, df2], join='inner', ignore_index=True)  # 只保留公共特征
print(res)
     b    c    d
0  0.0  0.0  0.0
1  0.0  0.0  0.0
2  0.0  0.0  0.0
3  1.0  1.0  1.0
4  1.0  1.0  1.0
5  1.0  1.0  1.0
res = pd.concat([df1, df2], axis=1, join_axes=[df1.index])  # 左右合并
print(res)
     a    b    c    d    b    c    d    e
1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN
2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
3  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
res = df1.append(df2, ignore_index=True, sort=False)
print(res)
     a    b    c    d    e
0  0.0  0.0  0.0  0.0  NaN
1  0.0  0.0  0.0  0.0  NaN
2  0.0  0.0  0.0  0.0  NaN
3  NaN  1.0  1.0  1.0  1.0
4  NaN  1.0  1.0  1.0  1.0
5  NaN  1.0  1.0  1.0  1.0
df3 = pd.DataFrame(np.ones((3, 4))*2, columns=['a', 'b', 'c', 'd']) 
res = df1.append([df2, df3], ignore_index=True, sort=False)
print(res)
     a    b    c    d    e
0  0.0  0.0  0.0  0.0  NaN
1  0.0  0.0  0.0  0.0  NaN
2  0.0  0.0  0.0  0.0  NaN
3  NaN  1.0  1.0  1.0  1.0
4  NaN  1.0  1.0  1.0  1.0
5  NaN  1.0  1.0  1.0  1.0
6  2.0  2.0  2.0  2.0  NaN
7  2.0  2.0  2.0  2.0  NaN
8  2.0  2.0  2.0  2.0  NaN
s1 = pd.Series([1, 2, 3,4], index=['a', 'b', 'c', 'd'])  # 一行数据
res = df1.append(s1, ignore_index=True)
print(res)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  2.0  3.0  4.0

7. 合并merge

import pandas as pd
left = pd.DataFrame({
     'key':['K0', 'K1', 'K2', 'K3'],
                    'A':['A0', 'A1', 'A2', 'A3'],
                    'B':['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({
     'key':['K0', 'K1', 'K2', 'K3'],
                    'A':['C0', 'C1', 'C2', 'C3'],
                    'B':['D0', 'D1', 'D2', 'D3']})
print(left)
print(right)
  key   A   B
0  K0  A0  B0
1  K1  A1  B1
2  K2  A2  B2
3  K3  A3  B3
  key   A   B
0  K0  C0  D0
1  K1  C1  D1
2  K2  C2  D2
3  K3  C3  D3
res = pd.merge(left, right, on='key')
print(res)
  key A_x B_x A_y B_y
0  K0  A0  B0  C0  D0
1  K1  A1  B1  C1  D1
2  K2  A2  B2  C2  D2
3  K3  A3  B3  C3  D3
left = pd.DataFrame({
     'key1':['K0', 'K0', 'K1', 'K2'],
                     'key2':['K0', 'K1', 'K0', 'K1'],
                    'A':['A0', 'A1', 'A2', 'A3'],
                    'B':['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({
     'key1':['K0', 'K1', 'K1', 'K2'],
                      'key2':['K0', 'K0', 'K0', 'K0'],
                    'A':['C0', 'C1', 'C2', 'C3'],
                    'B':['D0', 'D1', 'D2', 'D3']})
print(left)
print(right)
  key1 key2   A   B
0   K0   K0  A0  B0
1   K0   K1  A1  B1
2   K1   K0  A2  B2
3   K2   K1  A3  B3
  key1 key2   A   B
0   K0   K0  C0  D0
1   K1   K0  C1  D1
2   K1   K0  C2  D2
3   K2   K0  C3  D3
res = pd.merge(left, right, on=['key1', 'key2'], how='inner')  # how: left, right, outer, inner
print(res)
  key1 key2 A_x B_x A_y B_y
0   K0   K0  A0  B0  C0  D0
1   K1   K0  A2  B2  C1  D1
2   K1   K0  A2  B2  C2  D2
df1 = pd.DataFrame({
     'col1':[0,1],'col_left':['a', 'b']})
df2 = pd.DataFrame({
     'col1':[1, 2, 2],'col_right':[2, 2, 2]})
print(df1)
print(df2)
   col1 col_left
0     0        a
1     1        b
   col1  col_right
0     1          2
1     2          2
2     2          2
res = pd.merge(df1, df2, on='col1', how='outer', indicator=True)
print(res)
   col1 col_left  col_right      _merge
0     0        a        NaN   left_only
1     1        b        2.0        both
2     2      NaN        2.0  right_only
3     2      NaN        2.0  right_only
res = pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_col')  # 添加指示说明
print(res)
   col1 col_left  col_right indicator_col
0     0        a        NaN     left_only
1     1        b        2.0          both
2     2      NaN        2.0    right_only
3     2      NaN        2.0    right_only
left = pd.DataFrame({
     'A':['A0', 'A1', 'A2'],
                    'B':['B0', 'B1', 'B2']},
                   index=['K0', 'K1', 'K2'])
right = pd.DataFrame({
     'C':['C0', 'C1', 'C2'],
                    'D':['D0', 'D1', 'D2']},
                    index=['K0', 'K2', 'K3'])
print(left)
print(right)
     A   B
K0  A0  B0
K1  A1  B1
K2  A2  B2
     C   D
K0  C0  D0
K2  C1  D1
K3  C2  D2
res = pd.merge(left, right, left_index=True, right_index=True, how='outer')
print(res)
res = pd.merge(left, right, left_index=True, right_index=True, how='inner')
print(res)
      A    B    C    D
K0   A0   B0   C0   D0
K1   A1   B1  NaN  NaN
K2   A2   B2   C1   D1
K3  NaN  NaN   C2   D2
     A   B   C   D
K0  A0  B0  C0  D0
K2  A2  B2  C1  D1
boys = pd.DataFrame({
     'k':['K0', 'K1', 'K2'], 'age':[1, 2, 3]})
girls = pd.DataFrame({
     'k':['K0', 'K0', 'K2'], 'age':[4, 5, 6]})
print(boys)
print(girls)
    k  age
0  K0    1
1  K1    2
2  K2    3
    k  age
0  K0    4
1  K0    5
2  K2    6
res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girls'], how='inner')
print(res)
    k  age_boy  age_girls
0  K0        1          4
1  K0        1          5
2  K2        3          6
res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girls'], how='outer')
print(res)
    k  age_boy  age_girls
0  K0        1        4.0
1  K0        1        5.0
2  K1        2        NaN
3  K2        3        6.0

9. plot画图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.Series(np.random.randn(1000), index=np.arange(1000))  # 正态分布
data = data.cumsum()  # 累加
data.plot()
plt.show()

莫烦Python Numpy&Pandas 学习笔记_第1张图片

data  = pd.DataFrame(np.random.randn(1000, 4), index=np.arange(1000), columns=list("ABCD"))
print(data.head(5))  # 输出data的前5行数据
          A         B         C         D
0 -1.473072 -0.751053  0.131861 -0.693762
1 -0.947544 -0.147147 -0.556534  0.424414
2  0.388600 -1.637560  0.161108  0.756645
3  1.372510 -0.675933 -0.441797 -1.028523
4 -1.941062  1.263854 -1.704671  1.076629
data = data.cumsum()
ax = data.plot.scatter(x="A", y="B", color="DarkBlue", label="class 1")
data.plot.scatter(x="A", y="C", color="DarkGreen", label="class 2", ax = ax)
# plt: bat hist box kde area scatter hexbin pie
plt.show()

莫烦Python Numpy&Pandas 学习笔记_第2张图片

你可能感兴趣的:(机器学习,python,numpy,机器学习)