数据分析三剑客之 Pandas 基础教程

在这里插入图片描述


其余两剑客:
🗡 数据分析三剑客之 Numpy基础教程
🗡 数据分析三剑客之 Matplotlib 基础教程


目录

0.1 先导条件:

import numpy as np
import pandas as pd

1 创建对象:

1.1 Series:

pd.Series 是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组。轴标签统称为索引。

  • data 参数
  • index 索引 索引值必须是唯一的和散列的,与数据的长度相同。 默认np.arange(n)如果没有索引被传递。
  • dtype 输出的数据类型 如果没有,将推断数据类型
  • copy 复制数据 默认为false

1.1.1 通过列表创建Series:

data = ['1', 'a', 'A', '@', 100, 3.1415926, 'giao',[_ for _ in range(5)], np.nan, '么的对象']  # data可以有多种数据类型
s = pd.Series(data=data,
              index=[1, 2, 3.1415926, 4, [_ for _ in range(3)], 'a', 'B', '*', '这也算', 'index']  # index也一样
              )
print(s)
1                          1
2                          a
3.1415926                  A
4                          @
[0, 1, 2]                100
a                    3.14159
B                       giao
*            [0, 1, 2, 3, 4]
这也算                      NaN
index                   么的对象
dtype: object

1.1.2 通过字典创建Series:

ps = pd.Series({'A': 0, 'index': '*', '索引': [1, '*', 'qwer']})
print(ps)
A                   0
index               *
索引       [1, *, qwer]
dtype: object

1.1.3 为Series对象即索引添加名字:

ps.name = "对象名字"
ps.index.name = "索引名字"
print(ps)
索引名字
A                   0
index               *
索引       [1, *, qwer]
Name: 对象名字, dtype: object

1.2 DataFrame:

pd.DataFrame 是能够保存任何类型的数据(整数,字符串,浮点数,Python对象、Series、另一个DataFrame等)的二维标记数组。有行索引(index)和列索引(columns)。

  • data表示要传入的数据 ,包括 ndarray,series,map,lists,dict,constant和另一个DataFrame
  • index和columns 行索引和列索引 格式[‘x1’,‘x2’]
  • dtype:每列的类型
  • copy: 从input输入中拷贝数据。默认是false,不拷贝。

1.2.1 通过NumPy数组创建DataFrame:

df = pd.DataFrame(data=np.random.randn(6, 4),  # 通过numpy创建一个 6×4 的数组
                  index=['index', 2, '索引', 'b',[_ for _ in range(3)], '&'],  # index有6个元素,可以为不同类型
                  columns=['columns', 1, '%', '列']  # conlumns有4个元素,不能为list
                  )
df  # print(df)
columns1%
index0.435026-0.032693-0.563487-0.115501
20.1400050.3392030.7515110.508840
索引-0.0830630.6989021.7462271.169957
b0.7024061.1157460.2722750.247573
[0, 1, 2]-0.2970251.8129850.990014-1.267218
&1.2066361.5567160.028735-0.679610

1.2.2 通过字典创建DataFrame:

df2 = pd.DataFrame({'A': [_ for _ in range(4)],
                    'B': pd.Timestamp('20200317'),
                    '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'})
df2  # print(df2)
ABCDEF
002020-03-171.03testfoo
112020-03-171.03trainfoo
222020-03-171.03testfoo
332020-03-171.03trainfoo

1.2.3 通过嵌套字典创建自定义行列索引的DataFrame:

外面的是列索引,嵌套进去的是行索引

df3 = pd.DataFrame({"第1列": {"第1行": "a", "第2行": "b"},
                    "第2列": {"第1行": "*", "第2行": "?"}}
                   )
df3  # print(df3)
第1列第2列
第1行a*
第2行b?

2 查看数据:

先重新建个表:

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df  # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324

2.1 查看数据的头和尾:

df.head()  # print(df.head())  # 默认查看前(后)5个
ABCD
11234
25678
39101112
413141516
517181920
df.tail(2)  # print(df.tail(2))  # 也可以指定
ABCD
517181920
621222324

2.2 查看中间的细节:

2.2.1 查看索引名:

print(df.index)
Int64Index([1, 2, 3, 4, 5, 6], dtype='int64')

2.2.2 查看列名:

print(df.columns)
Index(['A', 'B', 'C', 'D'], dtype='object')

2.2.3 查看所有值:

print(df.values)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]]

2.2.4 查看行数/列数:

print(df.shape[0])  # 行数
print(df.shape[1])  # 列数
6
4

2.2.5 行/列求和:

print(df.sum(0))  # 行
print(df.sum(1))  # 列
A    66
B    72
C    78
D    84
dtype: int64
1    10
2    26
3    42
4    58
5    74
6    90
dtype: int64

2.2.6 数据类型:

print(df.dtypes)
A    int32
B    int32
C    int32
D    int32
dtype: object

2.3 查看数据的统计信息(describe):

查看数据按列的统计信息,可显示数据的数量、缺失值、最小最大数、平均值、分位数等信息

df.describe(

    percentiles=None,  #这个参数可以设定数值型特征的统计量,默认是[.25, .5, .75],也就是返回25%,50%,75%数据量时的数字,
                      但是这个可以修改,像这样:df['Parch'].describe(percentiles=[.2,.75, .8]) (但还是有50%时的数据)
    include=None,   
    
    exclude=None

)

df.describe()  # print(df.describe())
ABCD
count6.0000006.0000006.0000006.000000
mean11.00000012.00000013.00000014.000000
std7.4833157.4833157.4833157.483315
min1.0000002.0000003.0000004.000000
25%6.0000007.0000008.0000009.000000
50%11.00000012.00000013.00000014.000000
75%16.00000017.00000018.00000019.000000
max21.00000022.00000023.00000024.000000

2.4 数据转置:

df.T  # print(df.T)
123456
A159131721
B2610141822
C3711151923
D4812162024

2.5 数据排序:

两种方式:df. sort_index() df.sort_values()

df. sort_index()可以完成和df. sort_values()完全相同的功能,

但python更推荐用只用df. sort_index()对“根据行标签”和“根据列标签”排序,其他排序方式用df.sort_values()。

2.5.1 沿某一轴排序:

df.sort_index(

    axis:0按照行名排序;1按照列名排序,默认为0
    
    level:默认None,否则按照给定的level顺序排列---貌似并不是,文档
    
    ascending:默认True升序排列;False降序排列
    
    inplace:布尔型,是否用排序后的数据框替换现有的数据框,默认否
    
    kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’。似乎不用太关心。
    
    na_position:缺失值默认排在最后{"first","last"}
    
    by:按照某一列或几列数据进行排序

)

df.sort_index(ascending=False)  # print(df.sort_index(ascending=False))
ABCD
621222324
517181920
413141516
39101112
25678
11234

2.5.2 按照值进行排序:

df.sort_values(

    axis:{0 or ‘index’, 1 or ‘columns’}, 默认为0,按照列排序,即纵向排序;如果为1,则是横向排序
    
    by:str or list of str;如果axis=0,那么by="列名";如果axis=1,那么by="行名"
    
    ascending:布尔型,True则升序,如果by=['列名1','列名2'],则该参数可以是[True, False],即第一字段升序,第二个降序,默认True
    
    inplace:布尔型,是否用排序后的数据框替换现有的数据框,默认否
    
    kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}, 默认 ‘quicksort’,似乎不用太关心...
    
    na_position:{‘first’, ‘last’}, default ‘last’,默认缺失值排在最后面

)

df.sort_values(by='B', ascending=False) # print(df.sort_values(by='B', ascending=False))
ABCD
621222324
517181920
413141516
39101112
25678
11234

3 数据选择:

3.1 数据获取:(基本方法)

3.1.1 根据列名获取某一列的数据:

print("第一列的Series:")  # 根据列名查看某一列的值(返回Series)
print(df['A'])

print("第一列的Series(法2):")  # 效果同上
print(df.A)

print("第一列的值:")  # 根据列名查看某一列的值(返回numpy.ndarray)
print(df['A'].values)
第一列的Series:
1     1
2     5
3     9
4    13
5    17
6    21
Name: A, dtype: int32
第一列的Series(法2):
1     1
2     5
3     9
4    13
5    17
6    21
Name: A, dtype: int32
第一列的值:
[ 1  5  9 13 17 21]

3.1.2 切片获取某几行数据:

df[0:3]  # print(df[0:3])  # 按行号切片
ABCD
11234
25678
39101112
df[2:4]  # print( df[2:4])#按行名切片
# 由于这里我的行名是整型,所以按行名切片是的索引不能加引号
ABCD
39101112
413141516

3.2 根据名称选择数据(loc):

df.loc查找元素都是根据名称

3.2.1 行名查找:

print(df.loc[1])  # 注意,在这里,行名时整型,那么中括号里的索引也应该是整型,不能加引号
A    1
B    2
C    3
D    4
Name: 1, dtype: int32

3.2.2 切片获取某几行的数据:

其实就是选取所有列,再选择某几行

df.loc[2:3, ]  # print(df.loc[2:3, ])
ABCD
25678
39101112

3.2.3 切片获取某几列的数据:

其实就是选取所有行,再选择某几列

df.loc[:, ['A', 'B']]  # print(df.loc[:, ['A', 'B']])
AB
112
256
3910
41314
51718
62122

3.2.4 获取特定位置的数据:(标量)

就是结合上面两种方法

print(df.loc[1, 'A'])
print(df.at[1, 'A'])  # 效果一样
1
1

3.2.5 获取某几行某几列的数据:(连续)

df.loc[1:3, 'A':'C']  # print(df.loc[1:3, 'A':'C'])
ABC
1123
2567
391011

3.2.6 获取某几行某几列的数据:(离散)

df.loc[[1, 3], ['A', 'C']]# print(df.loc[[1, 3], ['A', 'C']])
AC
113
3911

3.2.7 降维返回某一行指定列的数据:

print(df.loc[2, ['B', 'C']])
B    6
C    7
Name: 2, dtype: int32

3.3 根据位置选择数据(iloc):

df.iloc查找元素都是根据位置(下标/索引)

3.3.1 行号查找:

print(df.iloc[0])
A    1
B    2
C    3
D    4
Name: 1, dtype: int32

3.3.2 切片获取某几行的数据:

df.iloc[3:5, :]  # print(df.iloc[3:5,:])
ABCD
413141516
517181920

3.3.3 切片获取某几列的数据:

df.iloc[:, 2:4]  # print(df.iloc[:,2:4])
CD
134
278
31112
41516
51920
62324

3.3.4 获取特定位置的数据:(标量)

此时可以将DataFrame看作一个二维数组

print(df.iloc[0, 0])
print(df.iat[0, 0])  # 效果一样
1
1

3.3.5 获取某几行某几列的数据:(连续)

df.iloc[0:2, 0:2]  # print(df.iloc[0:2, 0:2])
AB
112
256

3.3.6 获取某几行某几列的数据:(离散)

df.iloc[[0, 2], [0, 2]]  # print(df.iloc[[0, 2], [0, 2]])
AC
113
3911

3.4 布尔索引:

3.4.1 对某一列的值进行判断:

print(df.A>10)
1    False
2    False
3    False
4     True
5     True
6     True
Name: A, dtype: bool

3.4.2 以某一列的值为索引:

df[df.A > 10]  # print(df[df.A>10])
ABCD
413141516
517181920
621222324

3.4.3 从整个DataFrame中选择你想要的范围值:

df[df > 10]  # print(df[df>10])
ABCD
1NaNNaNNaNNaN
2NaNNaNNaNNaN
3NaNNaN11.012.0
413.014.015.016.0
517.018.019.020.0
621.022.023.024.0

3.4.4 判断某个元素是否在DataFrame中:

df.isin([1, 10, 20, 100, '1000'])  # print(df.isin([1,10,20,100,'1000']))
ABCD
1TrueFalseFalseFalse
2FalseFalseFalseFalse
3FalseTrueFalseFalse
4FalseFalseFalseFalse
5FalseFalseFalseTrue
6FalseFalseFalseFalse

3.4.5 判断某个元素是否在某一列中:

print(df.A.isin([1,13,100,'*']))
1     True
2    False
3    False
4     True
5    False
6    False
Name: A, dtype: bool

3.5 设置数据:

3.5.1 设置一个新列并设置索引:

s = pd.Series(["New{}".format(_)for _ in range(1, 7)], index=[_ for _ in range(4, 10)])
print(s)
4    New1
5    New2
6    New3
7    New4
8    New5
9    New6
dtype: object
df['E'] = s
df  # print(df)
# 如果Series中的值对应的索引再DataFrame中不存在,则新的这一列的值为NaN
ABCDE
11234NaN
25678NaN
39101112NaN
413141516New1
517181920New2
621222324New3

3.5.2 根据标签来设置值:

df.at[3, 'A'] = 1000
df  # print(df)
ABCDE
11234NaN
25678NaN
31000101112NaN
413141516New1
517181920New2
621222324New3

3.5.3 根据位置来设置值:

df.iat[3, 0] = 2000
df  # print(df)
ABCDE
11234NaN
25678NaN
31000101112NaN
42000141516New1
517181920New2
621222324New3

3.5.4 设置某一列的值:

df.loc[:, 'D'] = [_*10000 for _ in range(1, 7)]
df  # print(df)
ABCDE
112310000NaN
256720000NaN
31000101130000NaN
42000141540000New1
517181950000New2
621222360000New3

3.5.5 在过滤的同时进行赋值:

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df[df > 15] = -df
df  # print(df)
ABCD
11234
25678
39101112
4131415-16
5-17-18-19-20
6-21-22-23-24

3.5.6 重新设置索引:

df1 = df.reindex(index=[_ for _ in range(3, 8)],columns=['C', 'D', 'E'])  # 返回一个拷贝对象
df1  # print(df1)
# 如果新索引中包含原索引,则新索引数据和原索引一样,否则为NaN
CDE
311.012.0NaN
415.0-16.0NaN
5-19.0-20.0NaN
6-23.0-24.0NaN
7NaNNaNNaN

3.6 处理缺失数据:

3.6.1 删除含有缺失数据的行:

df[df % 7 == 0] = np.nan
df  # print(df)
ABCD
11.02.03.04
25.06.0NaN8
39.010.011.012
413.0NaN15.0-16
5-17.0-18.0-19.0-20
6NaN-22.0-23.0-24
df.dropna(how='any')  # print(df.dropna(how='any'))
ABCD
11.02.03.04
39.010.011.012
5-17.0-18.0-19.0-20

3.6.2 获取NaN位置:

pd.isnull(df)  # print(pd.isnull(df))
ABCD
1FalseFalseFalseFalse
2FalseFalseTrueFalse
3FalseFalseFalseFalse
4FalseTrueFalseFalse
5FalseFalseFalseFalse
6TrueFalseFalseFalse

3.6.3 填充缺失数据:

df.fillna("空虚")  # print(df.fillna("空虚"))
ABCD
11234
256空虚8
39101112
413空虚15-16
5-17-18-19-20
6空虚-22-23-24

4 基础操作:

4.1 行/列平均值:

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
print("每行平均值:")
print(df.mean())
print("每列平均值:")
print(df.mean(1))
每行平均值:
A    11.0
B    12.0
C    13.0
D    14.0
dtype: float64
每列平均值:
1     2.5
2     6.5
3    10.5
4    14.5
5    18.5
6    22.5
dtype: float64

4.2 加减乘除:

s = pd.Series([1, 2, 3, 4, 5, 6], index=[_ for _ in range(1, 7)])
print(s)
# 第一行各元素全部 加/减/乘/除 1
# 第一行各元素全部 加/减/乘/除 2
#           ...
1    1
2    2
3    3
4    4
5    5
6    6
dtype: int64
df.add(s, axis='index')  # print(df.add(s, axis='index')) # 加
ABCD
12345
278910
312131415
417181920
522232425
627282930
df.sub(s, axis='index')  # print(df.sub(s, axis='index')) # 减
ABCD
10123
23456
36789
49101112
512131415
615161718
df.mul(s, axis='index')  # print(df.mul(s, axis='index')) # 乘
ABCD
11234
210121416
327303336
452566064
5859095100
6126132138144
df.div(s, axis='index')  # print(df.div(s, axis='index')) # 除
ABCD
11.002.0000003.0000004.0
22.503.0000003.5000004.0
33.003.3333333.6666674.0
43.253.5000003.7500004.0
53.403.6000003.8000004.0
63.503.6666673.8333334.0

4.3 对数据应用函数:

4.3.1 将函数应用到由各行各列所形成的一维数组上(apply):

df.apply(

    func, #一个函数,多为lambda表达式
    
    axis=0, #默认为0,即以列为单位操作数据,返回一个新的行;若axis=1则为以行为单位操作数据,返回一个新的列
    
    broadcast=False,
    
    raw=False, 
    
    reduce=None, 
    
    args=(),
    
    **kwds

)

df['sum_行'] = df.apply(lambda x: x.sum(), axis=1)  # print(df)
df
ABCDsum_行
1123410
2567826
3910111242
41314151658
51718192074
62122232490
df.loc['sum_列'] = df.apply(lambda x: x.sum(), axis=0)  # print(df)
df
ABCDsum_行
1123410
2567826
3910111242
41314151658
51718192074
62122232490
sum_列66727884300

4.3.2 将函数应用到各个元素上(applymap):

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df.applymap(lambda x: x*100)  # print(df.applymap(lambda x:x*100))
ABCD
1100200300400
2500600700800
3900100011001200
41300140015001600
51700180019002000
62100220023002400

4.4 统计数据频次(value_counts):

4.4.1 在Series中的应用:

s = pd.Series(np.random.randint(0, 7, size=10))
print(s)
print()
print(s.value_counts())  # 统计Series的频次
0    1
1    6
2    3
3    0
4    5
5    3
6    3
7    6
8    0
9    1
dtype: int32

3    3
6    2
1    2
0    2
5    1
dtype: int64

4.4.2 在DataFrame中的应用:

df.apply(pd.value_counts) # print(df.apply(pd.value_counts))  # 统计DataFrame的元素频次
ABCD
11.0NaNNaNNaN
2NaN1.0NaNNaN
3NaNNaN1.0NaN
4NaNNaNNaN1.0
51.0NaNNaNNaN
6NaN1.0NaNNaN
7NaNNaN1.0NaN
8NaNNaNNaN1.0
91.0NaNNaNNaN
10NaN1.0NaNNaN
11NaNNaN1.0NaN
12NaNNaNNaN1.0
131.0NaNNaNNaN
14NaN1.0NaNNaN
15NaNNaN1.0NaN
16NaNNaNNaN1.0
171.0NaNNaNNaN
18NaN1.0NaNNaN
19NaNNaN1.0NaN
20NaNNaNNaN1.0
211.0NaNNaNNaN
22NaN1.0NaNNaN
23NaNNaN1.0NaN
24NaNNaNNaN1.0
print(df['A'].value_counts())  # 统计DataFrame某一列元素的频次
print()
print(df['A'].value_counts(normalize=True))  # 统计DataFrame某一列元素的频次(返回计数占比)
13    1
5     1
17    1
9     1
1     1
21    1
Name: A, dtype: int64

13    0.166667
5     0.166667
17    0.166667
9     0.166667
1     0.166667
21    0.166667
Name: A, dtype: float64

4.4.3 字符串处理:

s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'Dog', 'Cat'])
print(s.str.lower())
print()
print(s.str.upper())
0       a
1       b
2       c
3    aaba
4    baca
5     NaN
6    caba
7     dog
8     cat
dtype: object

0       A
1       B
2       C
3    AABA
4    BACA
5     NaN
6    CABA
7     DOG
8     CAT
dtype: object

4.5 删除数据(drop):

df.drop(

    labels=None,  # 标签或列表
    
    axis=0,  # axis=0:index  
           axis=1; column
    index=None,
    
    columns=None, 
    
    level=None,
    
    inplace=False, 
    
    errors='raise'

)

4.5.1 Series:

ps = pd.Series([1, 2, 3], ['A', 'B', 'C'])
print(ps)
A    1
B    2
C    3
dtype: int64
ps2 = ps.drop('B')
print(ps2)
A    1
C    3
dtype: int64

4.5.2 DataFrame:

df = pd.DataFrame(data=np.random.randn(5, 3),index=['a','b','c','d','e'])
df  # print(df)
012
a1.2338580.2751961.131398
b-0.9927250.8707380.120562
c0.6838360.1417041.243834
d-0.0330521.032105-0.395810
e-1.193372-0.3590420.150114
4.5.2.1 删除行:
df2 = df.drop('c')
df2  # print(df2)
012
a1.2338580.2751961.131398
b-0.9927250.8707380.120562
d-0.0330521.032105-0.395810
e-1.193372-0.3590420.150114
4.5.2.2 删除列:
df3 = df.drop(1, axis=1)
df3  # print(df3)
02
a1.2338581.131398
b-0.9927250.120562
c0.6838361.243834
d-0.033052-0.395810
e-1.1933720.150114
4.5.2.3 删除多行:
df = pd.DataFrame(data=np.random.randn(5, 3), index=['a', 'b', 'c', 'd', 'e'])
df2 = df.drop(['a', 'c'])
df2  # print(df2)
012
b1.125981-0.378187-1.802324
d0.310137-1.8624771.997893
e-0.005787-0.5748340.020197
4.5.2.4 删除多列:
df = pd.DataFrame(data=np.random.randn(5, 3), index=['a', 'b', 'c', 'd', 'e'])
df2 = df.drop([0, 2], axis=1)
df2  # print(df2)
1
a0.864516
b-0.387891
c1.437401
d1.268465
e1.168362

4.5.3 drop中的inplace:

对原数组作出修改并返回一个新数组,往往都有一个 inplace可选参数。如果手动设定为True(默认为False),那么原数组直接就被替换。也就是说,采用inplace=True之后,原数组名对应的内存值直接改变;而采用inplace=False之后,原数组名对应的内存值并不改变,需要将新的结果赋给一个新的数组或者覆盖原数组的内存位置。

df = pd.DataFrame(data=np.random.randn(5, 3),index=['a','b','c','d','e'])
df  # print(df)
012
a0.2687800.6767700.870340
b-0.0424130.287115-0.125729
c1.9178641.494957-0.183320
d0.214316-0.4893680.516577
e1.029021-0.8119971.115401
df.drop('b')
df  # print(df)
012
a0.2687800.6767700.870340
b-0.0424130.287115-0.125729
c1.9178641.494957-0.183320
d0.214316-0.4893680.516577
e1.029021-0.8119971.115401
df.drop('b', inplace=True)
df  # print(df)
012
a0.2687800.6767700.870340
c1.9178641.494957-0.183320
d0.214316-0.4893680.516577
e1.029021-0.8119971.115401

4.6 转字典格式(to_dict):

4.6.1 Series :

ps = pd.Series([1, 2, 3])
print(ps)
0    1
1    2
2    3
dtype: int64
ps1 = ps.to_dict()
print(type(ps1))
print(ps1)
<class 'dict'>
{0: 1, 1: 2, 2: 3}

4.6.2 DataFrame:

df = pd.DataFrame(np.random.randn(3, 4))
df  # print(df)
0123
0-1.506612-2.933778-0.440793-0.199159
11.7590940.6682981.6177680.478062
20.741909-0.647517-0.604264-0.432762
df2 = df.to_dict()
print(df2)
{0: {0: -1.5066115794537556, 1: 1.7590942894363175, 2: 0.7419087880835106}, 1: {0: -2.9337777444534576, 1: 0.6682981783861266, 2: -0.6475174504184045}, 2: {0: -0.4407929909426669, 1: 1.6177680605552671, 2: -0.6042642630578554}, 3: {0: -0.19915875027129182, 1: 0.47806199576280606, 2: -0.43276232283552823}}

5 数据合并:

5.1 数据拼接(concat):

pd.concat(

    objs,  # 需要连接的对象,eg [df1, df2] 
    
    axis=0,   # 表示在水平方向(row)进行连接 axis = 1, 表示在垂直方向(column)进行连接 
    
    join='outer',  # 表示index全部需要; inner,表示只取index重合的部分
    
    join_axes=None,   # 传入需要保留的index 
    
    ignore_index=False,  # 忽略需要连接的frame本身的index。当原本的index没有特别意义的时候可以使用 
    
    keys=None,          # 可以给每个需要连接的df一个label
    
    levels=None,
    
    names=None, 
    
    verify_integrity=False,
    
    copy=True

)

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324

5.1.1 行拼接:(纵向拼接)

df和df1的列索引相同

df1 = pd.DataFrame(data=np.arange(100, 124).reshape(6, 4), index=[_*1000 for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df1 # print(df1)
ABCD
1000100101102103
2000104105106107
3000108109110111
4000112113114115
5000116117118119
6000120121122123
pd.concat([df, df1], axis=0)  # print(pd.concat([df, df1],axis=0))
ABCD
11234
25678
39101112
413141516
517181920
621222324
1000100101102103
2000104105106107
3000108109110111
4000112113114115
5000116117118119
6000120121122123

5.1.2 列拼接:(横向拼接)

df和df2的行索引相同

df2 = pd.DataFrame(data=np.arange(100,124).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['E', 'F', 'G', 'H'])
df2 # print(df2)
EFGH
1100101102103
2104105106107
3108109110111
4112113114115
5116117118119
6120121122123
pd.concat([df, df2], axis=1)  # print(pd.concat([df, df2],axis=1))
ABCDEFGH
11234100101102103
25678104105106107
39101112108109110111
413141516112113114115
517181920116117118119
621222324120121122123

5.1.3 混合拼接:

两个索引不同的DataFrame用同样的方法拼接起来

pd.concat([df1, df2], axis=1)  # print(pd.concat([df1, df2],axis=1))
ABCDEFGH
1NaNNaNNaNNaN100.0101.0102.0103.0
2NaNNaNNaNNaN104.0105.0106.0107.0
3NaNNaNNaNNaN108.0109.0110.0111.0
4NaNNaNNaNNaN112.0113.0114.0115.0
5NaNNaNNaNNaN116.0117.0118.0119.0
6NaNNaNNaNNaN120.0121.0122.0123.0
1000100.0101.0102.0103.0NaNNaNNaNNaN
2000104.0105.0106.0107.0NaNNaNNaNNaN
3000108.0109.0110.0111.0NaNNaNNaNNaN
4000112.0113.0114.0115.0NaNNaNNaNNaN
5000116.0117.0118.0119.0NaNNaNNaNNaN
6000120.0121.0122.0123.0NaNNaNNaNNaN

5.2 数据关联(merge):

pd.merge(

    left,  # 拼接的左侧DataFrame对象
    
    right,  # 拼接的右侧DataFrame对象
    
    how='inner',  # One of [‘left’, ‘right’, ‘outer’, ‘inner’]
              默认inner。inner是取交集,outer取并集。
              比如left:[‘A’,‘B’,‘C’];right['A',‘C’,‘D’];
              inner取交集的话,left中出现的A会和right中出现的买一个A进行匹配拼接,
              如果没有是B,在right中没有匹配到,则会丢失。
              'outer’取并集,出现的A会进行一一匹配,没有同时出现的会将缺失的部分添加缺失值。
    on=None,  # 要加入的列或索引级别名称。 必须在左侧和右侧DataFrame对象中找到。
            如果未传递且left_index和right_index为False,则DataFrame中的列的交集将被推断为连接键。
    left_on=None,  # 左侧DataFrame中的列或索引级别用作键。 
                可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。
    right_on=None,  # 右侧DataFrame中的列或索引级别用作键。
                可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。
    left_index=False,  # 如果为True,则使用左侧DataFrame中的索引(行标签)作为其连接键。
                  对于具有MultiIndex(分层)的DataFrame,级别数必须与右侧DataFrame中的连接键数相匹配。
    right_index=False, # 如果为True,则使用右侧DataFrame中的索引(行标签)作为其连接键。
                  对于具有MultiIndex(分层)的DataFrame,级别数必须与右侧DataFrame中的连接键数相匹配。
    sort=True,   # 按字典顺序通过连接键对结果DataFrame进行排序。
               默认为True,设置为False将在很多情况下显着提高性能。
    suffixes=('_x', '_y'),   # 用于重叠列的字符串后缀元组。 默认为(‘x’,’ y’)
    
    copy=True, #  始终从传递的DataFrame对象复制数据(默认为True),即使不需要重建索引也是如此。
    
    indicator=False,  # 将一列添加到名为_merge的输出DataFrame,其中包含有关每行源的信息。_merge是分类类型,并且
                对于其合并键仅出现在“左”DataFrame中的观察值,取得值为left_only,
                对于其合并键仅出现在“右”DataFrame中的观察值为right_only,
                并且如果在两者中都找到观察点的合并键,则为left_only。
    validate=None

)

df1 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                    'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']})
df1  # print(df1)
keyAB
0K0A0B0
1K1A1B1
2K2A2B2
3K3A3B3
df2 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']})
df2  # print(df2)
keyCD
0K0C0D0
1K1C1D1
2K2C2D2
3K3C3D3
pd.merge(left=df1, right=df2,on='key') # print(pd.merge(left=df1,right=df2,on='key'))
keyABCD
0K0A0B0C0D0
1K1A1B1C1D1
2K2A2B2C2D2
3K3A3B3C3D3

5.3 数据添加(append):

df.append(

    other,  # DataFrame或Series / dict-like对象,或者这些要附加的数据的列表
    
    ignore_index=False,  # 布尔值,默认False。如果为真,就不会使用索引标签
    
    verify_integrity=False, 
    
    sort=None

)

5.3.1 给DataFrame添加行:

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324
df.append(df.iloc[0])  # print(df.append(df.iloc[0]))
ABCD
11234
25678
39101112
413141516
517181920
621222324
11234

5.3.2 将一个DataFrame添加到另一个DataFrame下:

5.3.2.1 列索引相同时:
df1 = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df1 # print(df1)
ABCD
11234
25678
39101112
413141516
517181920
621222324
df2 = pd.DataFrame(data=np.arange(100, 124).reshape(6, 4), index=[_*1000 for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df2 # print(df2)
ABCD
1000100101102103
2000104105106107
3000108109110111
4000112113114115
5000116117118119
6000120121122123
df1.append(df2)  # print(df1.append(df2))
ABCD
11234
25678
39101112
413141516
517181920
621222324
1000100101102103
2000104105106107
3000108109110111
4000112113114115
5000116117118119
6000120121122123
5.3.2.2 列索引不同时:
df1 = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df1 # print(df1)
ABCD
11234
25678
39101112
413141516
517181920
621222324
df2 = pd.DataFrame(data=np.arange(1,10).reshape(3, 3), index=[_*1000 for _ in range(1, 4)], columns=['A', 'F', 'G'])
df2 # print(df2)
AFG
1000123
2000456
3000789
df1.append(df2)  # print(df1.append(df2))
C:\Anaconda\lib\site-packages\pandas\core\frame.py:7123: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  sort=sort,
ABCDFG
112.03.04.0NaNNaN
256.07.08.0NaNNaN
3910.011.012.0NaNNaN
41314.015.016.0NaNNaN
51718.019.020.0NaNNaN
62122.023.024.0NaNNaN
10001NaNNaNNaN2.03.0
20004NaNNaNNaN5.06.0
30007NaNNaNNaN8.09.0

6 分组(groupby):

df.groupby(

    by = None, # 映射,功能,标签或标签列表
            用于确定分组依据的分组。
            如果by是一个函数,则会在对象索引的每个值上调用它。
            如果通过了dict或Series,则将使用Series或dict VALUES来确定组(将Series的值首先对齐;请参见.align()方法)。
            如果传递ndarray,则按原样使用这些值来确定组。
            标签或标签列表可以按自身中的列传递给分组。
            请注意,元组被解释为(单个)键。
    axis = 0,  # {0或'index',1或'columns'},默认0。沿行(0)或列(1)分割。
    
    level = None,  # int,级别名称或此类的序列,默认值无。
                如果该轴是MultiIndex(分层),则按一个或多个特定级别分组。
    as_index = True,# bool,默认为True
                对于聚合输出,返回以组标签作为索引的对象。仅与DataFrame输入相关。
                as_index = False实际上是“SQL风格”的分组输出。
    sort = True,  # bool,默认为True。 Sort组键。
                关闭此功能可获得更好的性能。
                请注意,这不会影响每个组中观察的顺序。 
                Groupby保留每个组中行的顺序。
    group_keys = True,  # 布尔值,默认为True。调用Apply时,将组键添加到索引以识别片段。
    
    squeeze = False, # bool,默认值False。尽可能减小返回类型的维数,否则返回一致的类型。
    
    observe = False,
    
    ** kwargs 

)

df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
                   'C': np.random.randn(8),
                   'D': np.random.randn(8)})
df  # print(df)
ABCD
0fooone-0.063574-1.394417
1barone-1.1093510.031867
2footwo0.8677320.244025
3barthree0.3259440.255166
4footwo0.6041190.782786
5bartwo0.989785-2.102038
6fooone-0.649526-0.829681
7foothree1.4774801.882377

分组并对分组后的结果求和

df.groupby('A').sum() # print(df.groupby('A').sum())
CD
A
bar0.206378-1.815004
foo2.2362310.685090

根据多个列进行分组可以如下操作

df.groupby(['A','B']).sum() # print(df.groupby(['A','B']).sum())
CD
AB
barone-1.1093510.031867
three0.3259440.255166
two0.989785-2.102038
fooone-0.713100-2.224097
three1.4774801.882377
two1.4718511.026811

7 改变数据形状:

7.1 多层次索引(MultiIndex):

tuples = list(zip(*[['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]))
print(tuples)
[('bar', 'one'), ('bar', 'two'), ('baz', 'one'), ('baz', 'two'), ('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')]
m_index = pd.MultiIndex.from_tuples(tuples, names=['first_index', 'second_index'])
print(m_index)
MultiIndex([('bar', 'one'),
            ('bar', 'two'),
            ('baz', 'one'),
            ('baz', 'two'),
            ('foo', 'one'),
            ('foo', 'two'),
            ('qux', 'one'),
            ('qux', 'two')],
           names=['first_index', 'second_index'])
df = pd.DataFrame(np.arange(1, 33).reshape(8, 4), index=m_index, columns=['A', 'B', 'C', 'D'])
df  # print(df)
ABCD
first_indexsecond_index
barone1234
two5678
bazone9101112
two13141516
fooone17181920
two21222324
quxone25262728
two29303132

7.2 将数据的行索引旋转为列索引(stack):

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324
stack1=df.stack()
print(stack1)
1  A     1
   B     2
   C     3
   D     4
2  A     5
   B     6
   C     7
   D     8
3  A     9
   B    10
   C    11
   D    12
4  A    13
   B    14
   C    15
   D    16
5  A    17
   B    18
   C    19
   D    20
6  A    21
   B    22
   C    23
   D    24
dtype: int32

7.3 将数据的列索引旋转为行索引(unstack):

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324
stack2=df.unstack()
print(stack2)
A  1     1
   2     5
   3     9
   4    13
   5    17
   6    21
B  1     2
   2     6
   3    10
   4    14
   5    18
   6    22
C  1     3
   2     7
   3    11
   4    15
   5    19
   6    23
D  1     4
   2     8
   3    12
   4    16
   5    20
   6    24
dtype: int32

unstack解压stack压缩之后的内容:

stack1.unstack()  # print(stack1.unstack())
ABCD
11234
25678
39101112
413141516
517181920
621222324
stack1.unstack(0)  # print(stack1.unstack(0)) # 指定旋转轴的层次
123456
A159131721
B2610141822
C3711151923
D4812162024

7.4 数据重塑(pivot):

pd.pivot(

    index,  #  行索引
    
    columns,  # 列索引
    
    values   # 值

)

df = pd.DataFrame(data=np.arange(1, 25).reshape(6, 4), index=[_ for _ in range(1, 7)], columns=['A', 'B', 'C', 'D'])
df # print(df)
ABCD
11234
25678
39101112
413141516
517181920
621222324
df.pivot(index='A',columns='B',values='C')
B2610141822
A
13.0NaNNaNNaNNaNNaN
5NaN7.0NaNNaNNaNNaN
9NaNNaN11.0NaNNaNNaN
13NaNNaNNaN15.0NaNNaN
17NaNNaNNaNNaN19.0NaN
21NaNNaNNaNNaNNaN23.0

7.5 数据透视表(pivot_table):

pd.pivot_table(

    data,   #  DataFrame
    
    values=None,   #  要聚合的列,可选
    
    index=None,  # 列,组合,数组或是他们的列表。
              如果传递数组,则其长度必须与数据长度相同。
              该列表可以包含任何其他类型(列表除外)。
              在数据透视表索引上进行分组的键。
              如果传递了数组,则其使用方式与列值相同。
    columns=None,  #  列,Grouper,数组或上一个列表。
              如果传递数组,则该数组必须与数据长度相同。
              该列表可以包含任何其他类型(列表除外)。
              在数据透视表列上进行分组的键。
              如果传递了数组,则其使用方式与列值相同。
    aggfunc='mean',   #  函数,函数列表,字典,默认numpy.mean
                如果传递了函数列表,则结果数据透视表将具有层次结构的列,其顶层是函数名称(从函数对象本身推断)。
                如果传递了dict,则键为列汇总,值是函数还是函数列表
    fill_value=None,   # 标量,默认为None值,用于将丢失的值替换为...
    
    margins=False,  # 布尔值,默认为False。添加所有行/列
    
    dropna=True,   #  布尔值,默认为True。不包括条目均为NaN的列
    
    margins_name='All'  #  字符串,默认为“All”。行/列的名称,当margins为True时将包含总计。

)

df = pd.DataFrame({'A': ['one', 'one', 'two', 'three']*3,
                   'B': ['A', 'B', 'C']*4,
                   'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
                   'D': np.arange(1,13),
                   'E': [_*100 for _ in np.arange(1,13)]})
df # print(df)
ABCDE
0oneAfoo1100
1oneBfoo2200
2twoCfoo3300
3threeAbar4400
4oneBbar5500
5oneCbar6600
6twoAfoo7700
7threeBfoo8800
8oneCfoo9900
9oneAbar101000
10twoBbar111100
11threeCbar121200
pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C']) # print(pd.pivot_table(df,values='D',index=['A','B'],columns=['C']))
Cbarfoo
AB
oneA10.01.0
B5.02.0
C6.09.0
threeA4.0NaN
BNaN8.0
C12.0NaN
twoANaN7.0
B11.0NaN
CNaN3.0

8 时间序列(date_range):

Pandas拥有易用、强大且高效的方法来在频率变换中执行重采样操作(例如:把秒级别的数据转换成5分钟级别的数据)。这通常在金融应用中使用,但不仅限于金融应用。

pd.date_range(

    start=None, 
    
    end=None, 
    
    periods=None,   #  固定时期,取值为整数或None
    
    freq='D',  #  日期偏移量,取值为string或DateOffset,默认为'D'(以日为单位)
              还有'y'(以年为单位)、'm'(以月为单位)、'h'(以时为单位)、'min'(以分为单位)、
              's'(以秒为单位)
    tz=None,
    
    normalize=False,  #  若参数为True表示将start、end参数值正则化到午夜时间戳
    
    name=None,   # 生成时间索引对象的名称,取值为string或None
    
    closed=None,   #  可以理解成在closed=None情况下返回的结果中,
                若closed=‘left’表示在返回的结果基础上,再取左开右闭的结果,
                若closed='right'表示在返回的结果基础上,再取做闭右开的结果
    **kwargs

)

8.1 生成一个时间序列:

periods指定开始往后多少天:

date_index = pd.date_range('2020-3-17', periods=30)  # 默认以日为单位
print(date_index)
DatetimeIndex(['2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20',
               '2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26', '2020-03-27', '2020-03-28',
               '2020-03-29', '2020-03-30', '2020-03-31', '2020-04-01',
               '2020-04-02', '2020-04-03', '2020-04-04', '2020-04-05',
               '2020-04-06', '2020-04-07', '2020-04-08', '2020-04-09',
               '2020-04-10', '2020-04-11', '2020-04-12', '2020-04-13',
               '2020-04-14', '2020-04-15'],
              dtype='datetime64[ns]', freq='D')

直接指定日期区间:

date_index = pd.date_range('2020-3-17','2020-3-29')  # 默认以日为单位
print(date_index)
DatetimeIndex(['2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20',
               '2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26', '2020-03-27', '2020-03-28',
               '2020-03-29'],
              dtype='datetime64[ns]', freq='D')

8.2 时间序列作为索引:

作为Series的索引:

ts = pd.Series(np.arange(1, 31), index=date_index)
print(ts)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-98-0eec7c2e916b> in <module>
----> 1 ts = pd.Series(np.arange(1, 31), index=date_index)
      2 print(ts)


C:\Anaconda\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    297                         raise ValueError(
    298                             "Length of passed values is {val}, "
--> 299                             "index implies {ind}".format(val=len(data), ind=len(index))
    300                         )
    301                 except TypeError:


ValueError: Length of passed values is 30, index implies 13

作为DataFrame的索引:

pd.DataFrame(data=np.arange(1, 26).reshape(5, 5),
             index=pd.date_range('2020-3-17', periods=5),
             columns=pd.date_range('2000-4-1', periods=5))
# print(pd.DataFrame(data=np.arange(1,26).reshape(5,5),index=pd.date_range('2020-3-17',periods=5),columns=pd.date_range('2000-4-1',periods=5)))
2000-04-012000-04-022000-04-032000-04-042000-04-05
2020-03-1712345
2020-03-18678910
2020-03-191112131415
2020-03-201617181920
2020-03-212122232425

9 标签数据:

df = pd.DataFrame({'id': [1, 2, 3, 4, 5, 6], 'raw_grade': ['a', 'b', 'b', 'a', 'a', 'e']})
df
idraw_grade
01a
12b
23b
34a
45a
56e

将原始数据转换成标签数据:

df['grade'] = df['raw_grade'].astype('category')
print(df['grade'])
0    a
1    b
2    b
3    a
4    a
5    e
Name: grade, dtype: category
Categories (3, object): [a, b, e]
df # print(df)
idraw_gradegrade
01aa
12bb
23bb
34aa
45aa
56ee

将标签重命名成更有意义的名字:

df['grade'].cat.categories = ['very good', 'good', 'very bad']
print(df['grade'])
0    very good
1         good
2         good
3    very good
4    very good
5     very bad
Name: grade, dtype: category
Categories (3, object): [very good, good, very bad]
df # print(df)
idraw_gradegrade
01avery good
12bgood
23bgood
34avery good
45avery good
56every bad

重排序标签并且同时增加缺失的标签:

df['grade'] = df['grade'].cat.set_categories(['very bad', 'bad', 'medium', 'good', 'very good'])
print(df['grade'])
0    very good
1         good
2         good
3    very good
4    very good
5     very bad
Name: grade, dtype: category
Categories (5, object): [very bad, bad, medium, good, very good]

根据标签排序:

而非字典序

df.sort_values(by='grade') # print(df.sort_values(by='grade'))
idraw_gradegrade
56every bad
12bgood
23bgood
01avery good
34avery good
45avery good

对标签分组:

同样会显示空标签

print(df.groupby('grade').size())
grade
very bad     1
bad          0
medium       0
good         2
very good    3
dtype: int64

10 绘图:

需要import matplotlib.pyplot as plt

matplotlib绘图:数据分析三剑客之 Matplotlib 基础教程

import matplotlib.pyplot as plt

10.1 折线图(plot):

10.1.1 Series:

ts=pd.Series(np.random.randn(1000),index=list(range(1,1001)))
print(ts)
1       0.467369
2       0.657971
3       1.671581
4      -0.280581
5       1.662911
          ...   
996     0.167373
997    -1.364028
998    -0.269425
999     0.413813
1000    1.025991
Length: 1000, dtype: float64

正常图像:

ts.plot()
plt.show()

在这里插入图片描述

累计图像:

ts.cumsum().plot()
plt.show()

在这里插入图片描述

10.1.2 DataFrame:

df = pd.DataFrame(np.random.randn(1000,4),index=list(range(1,1001)),columns=['A','B','C','D'])
df  # print(df)
ABCD
10.8760692.065587-0.044876-1.551308
2-0.036537-2.316728-0.5376990.168699
31.629338-1.552489-0.9433070.657343
4-0.214209-0.538340-0.399330-0.726052
50.268486-1.328338-0.7425420.223678
...............
996-0.644218-0.0816721.538658-1.647879
997-2.601843-0.8441470.0851471.996194
998-0.8256430.3303881.0151131.195541
9991.4494900.9663050.847445-0.008207
10000.1261650.882948-0.1685491.732745

1000 rows × 4 columns

正常图像:

df.plot()
plt.show()

在这里插入图片描述

累计图像:

df.cumsum().plot()
plt.show()

在这里插入图片描述

10.2 散点图(scatter):

只能用于DataFrame

df.plot.scatter(x='A',y='B',color='c')
plt.show()

在这里插入图片描述

层叠显示:

pic1=df.plot.scatter(x='A',y='B',color='c',label='B')
pic2=df.plot.scatter(x='A', y='C', color='r', label='C', ax=pic1)
pic3=df.plot.scatter(x='A', y='D', color='b', label='D', ax=pic1)
plt.show()

在这里插入图片描述

10.3 柱状图(bar/barh):

df = pd.DataFrame(np.random.randn(10,2),index=list(range(1,11)),columns=['A','B'])
df # print(df)
AB
1-0.1452070.277669
20.2849781.639432
3-0.0774860.056532
41.2510531.289330
50.584433-0.995018
60.325838-0.370692
71.1836861.217381
8-0.581146-1.273807
90.481801-0.442896
10-1.9012830.424851

10.3.1 竖直柱状图:

df.plot.bar()
plt.show()

在这里插入图片描述

10.3.2 水平柱状图:

df.plot.barh()
plt.show()

在这里插入图片描述

10.3.3 堆叠柱状图:

df.plot.bar(stacked=True)
plt.show()

在这里插入图片描述

10.4 直方图(hist):

df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df.plot.hist(alpha=0.5)
plt.show()

在这里插入图片描述

10.5 箱型图(box):

df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
plt.show()

在这里插入图片描述

10.6 面积图(area):

df = pd.DataFrame(np.arange(1,41).reshape(10,4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
plt.show()

在这里插入图片描述

10.7 六角形箱体图(hexbin):

df = pd.DataFrame(np.random.randn(2000, 2), columns=['a', 'b'])
df.plot.hexbin(x='a', y='b', gridsize=10)
plt.show()

在这里插入图片描述

10.8 饼图(pie):

10.8.1 Series:

ps=pd.Series([_ for _ in range(11)])
ps.plot.pie()
plt.show()

在这里插入图片描述

10.8.2 DataFrame:

df = pd.DataFrame(np.arange(1, 21).reshape(10, 2), index=["index{}".format(_)for _ in range(1, 11)], columns=['A', 'B'])
df.plot.pie(subplots=True,  autopct='%.2f', legend=False)
plt.axis('equal')
plt.show()

在这里插入图片描述

10.9 密度图(kde):

ps = pd.Series(np.random.randn(1000))
ps.plot.kde()
plt.show()

在这里插入图片描述

10.10 一个综合例子:

(来源网络,但是源头在哪里我也说不清…)

data = [[66386, 174296,  75131, 577908,  32015],
        [58230, 381139,  78045,  99308, 160454],
        [89135,  80552, 152558, 497981, 603535],
        [78415,  81858, 150656, 193263,  69638],
        [139361, 331509, 343164, 781380,  52269]]
columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
df = pd.DataFrame(data, columns=('Freeze', 'Wind', 'Flood', 'Quake', 'Hail'),
                  index=['%d year' % x for x in (100, 50, 20, 10, 5)])
df.plot(kind='bar', grid=True, colormap='Blues_r',
        stacked=True, figsize=(8, 3))
plt.table(cellText=data,
          cellLoc='center',
          cellColours=None,
          rowLabels=rows,
          rowColours=plt.cm.BuPu(np.linspace(0, 0.5, 5))[::-1],
          colLabels=columns,
          colColours=plt.cm.Reds(np.linspace(0, 0.5, 5))[::-1],
          rowLoc='right',
          loc='bottom')
plt.xticks([])
plt.show()

在这里插入图片描述

11 数据获取与导出:

df = pd.DataFrame(data=np.random.randn(10, 10))
df  # print(df)
0123456789
01.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
1-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
20.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
31.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
4-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
5-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
60.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
7-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
8-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
90.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.1 json:

11.1.1 保存为json格式:

dfj = df.to_json("foo.txt")

在这里插入图片描述

11.1.2 从json中读取数据:

pds = pd.read_json("foo.txt")
pds  # print(pds)
0123456789
01.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
1-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
20.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
31.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
4-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
5-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
60.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
7-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
8-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
90.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.2 粘贴板(clipboard):

11.2.1 保存到粘贴板:

df.to_clipboard()

(因为打开剪贴板的时候不能截屏,所以我就拍照了…)
在这里插入图片描述

11.2.2 从粘贴板中读取数据:

df1 = pd.read_clipboard()
df1  # print(df1)
0123456789
01.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
1-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
20.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
31.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
4-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
5-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
60.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
7-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
8-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
90.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.3 csv

11.3.1 保存到csv:

df.to_csv('foo.csv')

在这里插入图片描述

11.3.2 从csv中读取数据:

df = pd.read_csv('foo.csv')
df  # print(df)
Unnamed: 00123456789
001.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
11-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
220.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
331.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
44-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
55-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
660.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
77-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
88-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
990.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.4 HDF5:

生成HDF5存储(需要安装tables库 pip install tables

11.4.1 保存到HDF5:

df.to_hdf('foo.h5','df')

11.4.2 从HDF5存储中读入数据:

df=pd.read_hdf('foo.h5','df')
df # print(df)
Unnamed: 00123456789
001.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
11-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
220.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
331.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
44-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
55-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
660.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
77-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
88-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
990.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.5 excel:

生成Excel文件(需要安装openpyxl库 pip install openpyxl

11.5.1 保存到excel:

df.to_excel('foo.xlsx',sheet_name='Sheet01')

在这里插入图片描述

11.5.2 从excel中读取数据:

df=pd.read_excel('foo.xlsx', 'Sheet01',index_col=None, na_values=['NA'])
df # print(df)
Unnamed: 0Unnamed: 0.10123456789
0001.068240-1.151127-0.7548910.4140210.638704-0.020647-0.448307-0.3829251.309449-1.166587
111-1.2819212.1326710.733787-0.054124-1.147173-2.450771-0.1008200.848969-0.1075130.785965
2220.4087190.915703-0.409178-0.244410-1.155077-0.0506310.376198-0.5352420.9589510.263776
3331.587115-0.111936-0.291101-1.616078-1.6371450.5209310.6082931.659118-1.352053-0.742237
444-0.416936-1.201720-0.536394-2.332946-1.145977-0.361430-0.1739242.1081831.2258511.183785
555-1.0382600.601028-1.109006-1.5285020.2780050.476453-0.232497-1.6801610.0327450.518991
6660.0060070.7563360.554774-0.1826110.1175810.091906-1.1844220.6216870.247616-0.243130
777-1.1052600.6020790.267793-0.564271-1.596596-0.959563-1.4285160.000715-0.7695730.537807
888-0.0159890.331866-0.106719-0.3599500.6300970.5986590.390666-0.7107340.8618560.060555
9990.615639-1.112035-0.0036130.252591-0.674543-0.1368061.322243-0.029689-0.7465842.128872

11.6 html:

11.6.1 保存到html:

df.to_html("foo.html")

在这里插入图片描述

11.6.2 从html中读取数据:

df1=pd.read_html('foo.html')
df1 # print(df)
[   Unnamed: 0  Unnamed: 0.1  Unnamed: 0.1.1         0         1         2  \
 0           0             0               0  1.068240 -1.151127 -0.754891   
 1           1             1               1 -1.281921  2.132671  0.733787   
 2           2             2               2  0.408719  0.915703 -0.409178   
 3           3             3               3  1.587115 -0.111936 -0.291101   
 4           4             4               4 -0.416936 -1.201720 -0.536394   
 5           5             5               5 -1.038260  0.601028 -1.109006   
 6           6             6               6  0.006007  0.756336  0.554774   
 7           7             7               7 -1.105260  0.602079  0.267793   
 8           8             8               8 -0.015989  0.331866 -0.106719   
 9           9             9               9  0.615639 -1.112035 -0.003613   
 
           3         4         5         6         7         8         9  
 0  0.414021  0.638704 -0.020647 -0.448307 -0.382925  1.309449 -1.166587  
 1 -0.054124 -1.147173 -2.450771 -0.100820  0.848969 -0.107513  0.785965  
 2 -0.244410 -1.155077 -0.050631  0.376198 -0.535242  0.958951  0.263776  
 3 -1.616078 -1.637145  0.520931  0.608293  1.659118 -1.352053 -0.742237  
 4 -2.332946 -1.145977 -0.361430 -0.173924  2.108183  1.225851  1.183785  
 5 -1.528502  0.278005  0.476453 -0.232497 -1.680161  0.032745  0.518991  
 6 -0.182611  0.117581  0.091906 -1.184422  0.621687  0.247616 -0.243130  
 7 -0.564271 -1.596596 -0.959563 -1.428516  0.000715 -0.769573  0.537807  
 8 -0.359950  0.630097  0.598659  0.390666 -0.710734  0.861856  0.060555  
 9  0.252591 -0.674543 -0.136806  1.322243 -0.029689 -0.746584  2.128872  ]
  • 23
    点赞
  • 139
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值