当前位置: 移动技术网 > IT编程>脚本编程>Python > 荐 Pandas库入门

荐 Pandas库入门

2020年07月11日  | 移动技术网IT编程  | 我要评论

在这里插入图片描述

Pandas库的介绍

1.Pandas是Python第三方库,提供高性能易用数据类型和分析工具。
2.Pandas基于NumPy实现,常与NumPy和Matplotlib一同使用。
3.Pandas库的引用

import pandas as pd

4.Pandas库的理解
①两个数据类型:Series,DataFrame
②基于上述数据类型的各类操作:
基本操作、运算操作、特征类操作、关联类操作
在这里插入图片描述

Pandas库的Series类型

1.Series类型的介绍
  • Series是一维带“标签”数组。
  • Series类型由一组数据及与之相关的数据索引组成。

自动索引

import pandas as pd

a = pd.Series([9,8,7,6])

a
Out[3]: 
0    9
1    8
2    7
3    6
dtype: int64

自定义索引

b = pd.Series([9,8,7,6], index=['a','b','c','d'])

b
Out[6]: 
a    9
b    8
c    7
d    6
dtype: int64

b = pd.Series([9,8,7,6], ['a','b','c','d']) #作为第二个参数,可以省略index=

b
Out[8]: 
a    9
b    8
c    7
d    6
dtype: int64
2.Series类型的创建

Series类型可以由如下类型创建:
①Python列表,index与列表元素个数一致。
②标量值,index表达Series类型的尺寸。
③Python字典,键值对中的“键”是索引,index从字典中进行选择操作。
④ndarray,索引和数据都可以通过ndarray类型创建。
⑤其他函数,range()函数等。

(1)从标量创建

s = pd.Series(9, index=['a','b','c'])

s
Out[10]: 
a    9
b    9
c    9
dtype: int64

(2)从字典类型创建

d = pd.Series({'a':9, 'b':8, 'c':7})

d
Out[12]: 
a    9
b    8
c    7
dtype: int64

d = pd.Series({'a':9, 'b':8, 'c':7},index=['c','a','b','d'])

d
Out[14]: 
c    7.0
a    9.0
b    8.0
d    NaN
dtype: float64

(3)从ndarray类型创建

n = pd.Series(np.arange(5))

n
Out[17]: 
0    0
1    1
2    2
3    3
4    4
dtype: int32

n = pd.Series(np.arange(5), index=np.arange(9,4,-1))

n
Out[19]: 
9    0
8    1
7    2
6    3
5    4
dtype: int32
3.Series类型的基本操作

(1)Series类型包括index和values两部分

b = pd.Series([9,8,7,6], ['a','b','c','d'])

b
Out[21]: 
a    9
b    8
c    7
d    6
dtype: int64

b.index #获得索引
Out[22]: Index(['a', 'b', 'c', 'd'], dtype='object')

b.values #获得数据
Out[23]: array([9, 8, 7, 6], dtype=int64)

b['b']
Out[24]: 8
#自动索引和自定义索引并存,但不能混用
b[1]
Out[25]: 8

b[['c', 'd', 'a']]
Out[28]: 
c    7
d    6
a    9
dtype: int64

(2)Series类型的操作类似ndarray类型

  • 索引方法相同,采用[ ]。
  • NumPy中运算和操作可用于Series类型。
  • 可以通过自定义索引的列表进行切片。
  • 可以通过自动索引进行切片,如果存在自定义索引,则一同被切片。
b = pd.Series([9,8,7,6], ['a','b','c','d'])

b
Out[33]: 
a    9
b    8
c    7
d    6
dtype: int64

b[3]
Out[34]: 6

b[:3] #得到的还是Series类型
Out[35]: 
a    9
b    8
c    7
dtype: int64

b[b>b.median()]
Out[36]: 
a    9
b    8
dtype: int64

np.exp(b)
Out[37]: 
a    8103.083928
b    2980.957987
c    1096.633158
d     403.428793
dtype: float64

(3)Series类型的操作类似Python字典类型

  • 通过自定义索引访问
  • 保留字in操作
  • 使用.get()方法
b = pd.Series([9,8,7,6], ['a','b','c','d'])

b['b']
Out[39]: 8

9 in b
Out[40]: False

'c' in b
Out[41]: True

b.get('f', 100)
Out[42]: 100
4.Series类型的对齐操作
a = pd.Series([1,2,3], ['c','d','e'])

b = pd.Series([9,8,7,6], ['a','b','c','d'])

a + b
Out[46]: 
a    NaN
b    NaN
c    8.0
d    8.0
e    NaN
dtype: float64
#Series类型是根据索引运算的,如果有一个没有该索引,则运算为空
5.Series类型的name属性

Series对象和索引都可以有一个名字,存储在属性.name中。

b = pd.Series([9,8,7,6], ['a','b','c','d'])

b.name

b.name = 'Series对象'

b.name
Out[50]: 'Series对象'

b.index.name = '索引列'

b
Out[52]: 
索引列
a    9
b    8
c    7
d    6
Name: Series对象, dtype: int64
6.Series类型的修改

Series对象可以随时修改并即刻生效。

b = pd.Series([9,8,7,6], ['a','b','c','d'])

b['a'] = 15

b.name = "Series"

b
Out[56]: 
a    15
b     8
c     7
d     6
Name: Series, dtype: int64

b.name = "New Series"

b['b','c'] = 20

b
Out[59]: 
a    15
b    20
c    20
d     6
Name: New Series, dtype: int64

Pandas库的DataFrame类型

1.DataFrame类型介绍
  • DataFrame是二维带“标签”数组。
  • DataFrame类型由共同相同索引的一组列组成。

在这里插入图片描述
在这里插入图片描述

  • DataFrame是一个表格型的数据类型,每列值类型可以不同。
  • DataFrame既有行索引、也有列索引。
  • DataFrame常用于表达二维数据,但可以表达多维数据。
2.DataFrame类型创建

DataFrame类型可以由如下类型创建:
①二维ndarray对象
②由一维ndarray、列表、字典、元组或Series构成的字典
③Series类型
④其他的DataFrame类型

(1)从二维ndarray对象创建

import pandas as pd

import numpy as np

d = pd.DataFrame(np.arange(10).reshape(2,5))

d
Out[4]: #生成自动行索引,自动列索引
   0  1  2  3  4
0  0  1  2  3  4
1  5  6  7  8  9

(2)从一维nadarray对象字典创建

dt = {'one':pd.Series([1,2,3], index=['a','b','c']),
      'two':pd.Series([9,8,7,6], index=['a','b','c','d'])}

d = pd.DataFrame(dt)

d
Out[8]: 
   one  two
a  1.0    9
b  2.0    8
c  3.0    7
d  NaN    6

pd.DataFrame(dt, index=['b','c','d'], columns=['two','three'])
Out[10]:  #数据根据行列索引自动补齐
   two three
b    8   NaN
c    7   NaN
d    6   NaN

(3)从列表类型创建

dl = {'one':[1,2,3,4], 'two':[9,8,7,6]}

d = pd.DataFrame(dl, index=['a','b','c','d'])

d
Out[13]: 
   one  two
a    1    9
b    2    8
c    3    7
d    4    6

示例
在这里插入图片描述

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5,101.2,101.3,102.0,100.1],
      '同比':[120.7,127.3,119.4,140.9,101.4],
      '定基':[121.4,127.8,120.0,145.5,101.6]}

d = pd.DataFrame(dl, index=['c1','c2','c3','c4','c5'])

d
Out[16]: 
    城市     环比     同比     定基
c1  北京  101.5  120.7  121.4
c2  上海  101.2  127.3  127.8
c3  广州  101.3  119.4  120.0
c4  深圳  102.0  140.9  145.5
c5  沈阳  100.1  101.4  101.6

d.index
Out[17]: Index(['c1', 'c2', 'c3', 'c4', 'c5'], dtype='object')

d.columns
Out[18]: Index(['城市', '环比', '同比', '定基'], dtype='object')

d.values
Out[19]: 
array([['北京', 101.5, 120.7, 121.4],
       ['上海', 101.2, 127.3, 127.8],
       ['广州', 101.3, 119.4, 120.0],
       ['深圳', 102.0, 140.9, 145.5],
       ['沈阳', 100.1, 101.4, 101.6]], dtype=object)

d['同比']
Out[20]: 
c1    120.7
c2    127.3
c3    119.4
c4    140.9
c5    101.4
Name: 同比, dtype: float64

d.loc['c2']
Out[30]: 
城市       上海
环比    101.2
同比    127.3
定基    127.8
Name: c2, dtype: object

d['同比']['c2']
Out[31]: 127.3

Pandas库的数据类型操作

如何改变Series和DataFrame对象?
①增加或重排:重新索引
②删除:drop

1.重新索引

.reindex()能够改变或重排Series和DataFrame索引
在这里插入图片描述

import pandas as pd

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5,101.2,101.3,102.0,100.1],
      '同比':[120.7,127.3,119.4,140.9,101.4],
      '定基':[121.4,127.8,120.0,145.5,101.6]}

d = pd.DataFrame(dl, index=['c1','c2','c3','c4','c5'])

d
Out[6]: 
    城市     环比     同比     定基
c1  北京  101.5  120.7  121.4
c2  上海  101.2  127.3  127.8
c3  广州  101.3  119.4  120.0
c4  深圳  102.0  140.9  145.5
c5  沈阳  100.1  101.4  101.6

newc = d.columns.insert(4,'新增')

newd = d.reindex(columns=newc, fill_value=200)

newd
Out[9]: 
    城市     环比     同比     定基   新增
c1  北京  101.5  120.7  121.4  200
c2  上海  101.2  127.3  127.8  200
c3  广州  101.3  119.4  120.0  200
c4  深圳  102.0  140.9  145.5  200
c5  沈阳  100.1  101.4  101.6  200

注意:Series和DataFrame的索引是Index类型,Index对象是不可修改类型。

2.索引类型的常用方法

在这里插入图片描述

d
Out[10]: 
    城市     环比     同比     定基
c1  北京  101.5  120.7  121.4
c2  上海  101.2  127.3  127.8
c3  广州  101.3  119.4  120.0
c4  深圳  102.0  140.9  145.5
c5  沈阳  100.1  101.4  101.6

nc = d.columns.delete(2)

nc
Out[12]: Index(['城市', '环比', '定基'], dtype='object')

ni = d.index.insert(5,'c0')

ni
Out[14]: Index(['c1', 'c2', 'c3', 'c4', 'c5', 'c0'], dtype='object')

nd = d.reindex(index=ni, columns=nc).ffill()

nd
Out[18]: 
    城市     环比     定基
c1  北京  101.5  121.4
c2  上海  101.2  127.8
c3  广州  101.3  120.0
c4  深圳  102.0  145.5
c5  沈阳  100.1  101.6
c0  沈阳  100.1  101.6
3.删除指定索引对象

.drop()能够删除Series和DataFrame指定行或列索引

a = pd.Series(['9','8','7','6'], index=['a','b','c','d'])

a
Out[20]: 
a    9
b    8
c    7
d    6
dtype: object

a.drop(['b','c'])
Out[21]: 
a    9
d    6
dtype: object

d
Out[22]: 
    城市     环比     同比     定基
c1  北京  101.5  120.7  121.4
c2  上海  101.2  127.3  127.8
c3  广州  101.3  119.4  120.0
c4  深圳  102.0  140.9  145.5
c5  沈阳  100.1  101.4  101.6

d.drop('c5') #默认操作为0轴
Out[23]: 
    城市     环比     同比     定基
c1  北京  101.5  120.7  121.4
c2  上海  101.2  127.3  127.8
c3  广州  101.3  119.4  120.0
c4  深圳  102.0  140.9  145.5

d.drop('同比', axis=1)
Out[24]: 
    城市     环比     定基
c1  北京  101.5  121.4
c2  上海  101.2  127.8
c3  广州  101.3  120.0
c4  深圳  102.0  145.5
c5  沈阳  100.1  101.6

Pandas库的数据类型运算

1.数值类型的算术运算

算术运算法则:

  • 算术运算根据行列索引,补齐后运算,运算默认产生浮点数。
  • 补齐时缺项填充NaN (空值)。
  • 二维和一维、一维和零维间为广播运算(比如2和一维数组每一个元素运算)。
  • 采用+ - * / 符号进行的二元运算产生新的对象。
import pandas as pd

import numpy as np

a = pd.DataFrame(np.arange(12).reshape(3,4))

a
Out[28]: 
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

b = pd.DataFrame(np.arange(20).reshape(4,5))

b
Out[30]: 
    0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

a + b #自动补齐,缺项补NaN
Out[31]: 
      0     1     2     3   4
0   0.0   2.0   4.0   6.0 NaN
1   9.0  11.0  13.0  15.0 NaN
2  18.0  20.0  22.0  24.0 NaN
3   NaN   NaN   NaN   NaN NaN

a * b
Out[32]: 
      0     1      2      3   4
0   0.0   1.0    4.0    9.0 NaN
1  20.0  30.0   42.0   56.0 NaN
2  80.0  99.0  120.0  143.0 NaN
3   NaN   NaN    NaN    NaN NaN

方法形式的运算
在这里插入图片描述

b.add(a, fill_value=100) #fill_value参数替代NaN,替代后参与运算
Out[35]: 
       0      1      2      3      4
0    0.0    2.0    4.0    6.0  104.0
1    9.0   11.0   13.0   15.0  109.0
2   18.0   20.0   22.0   24.0  114.0
3  115.0  116.0  117.0  118.0  119.0

a.mul(b, fill_value=0)
Out[36]: 
      0     1      2      3    4
0   0.0   1.0    4.0    9.0  0.0
1  20.0  30.0   42.0   56.0  0.0
2  80.0  99.0  120.0  143.0  0.0
3   0.0   0.0    0.0    0.0  0.0
c = pd.Series(np.arange(4))

c
Out[38]: 
0    0
1    1
2    2
3    3
dtype: int32

c - 10
Out[39]: 
0   -10
1    -9
2    -8
3    -7
dtype: int32

b - c #不同维度为广播运算,一维Series默认在轴1参与运算
Out[40]: 
      0     1     2     3   4
0   0.0   0.0   0.0   0.0 NaN
1   5.0   5.0   5.0   5.0 NaN
2  10.0  10.0  10.0  10.0 NaN
3  15.0  15.0  15.0  15.0 NaN

b.sub(c, axis=0) #使用运算方法可以令一维Series参与轴0运算
Out[41]: 
    0   1   2   3   4
0   0   1   2   3   4
1   4   5   6   7   8
2   8   9  10  11  12
3  12  13  14  15  16
2.数值类型的比较运算

比较运算法则:

  • 比较运算只能比较相同索引的元素,不进行补齐。
  • 二维和一维、一维和零维间为广播运算。
  • 采用> < >= <= == !=等符号进行的二元运算产生布尔对象。
a = pd.DataFrame(np.arange(12).reshape(3,4))

a
Out[43]: 
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

d = pd.DataFrame(np.arange(12,0,-1).reshape(3,4))

d
Out[45]: 
    0   1   2  3
0  12  11  10  9
1   8   7   6  5
2   4   3   2  1

a > d #同维度运算,尺寸要一致
Out[46]: 
       0      1      2      3
0  False  False  False  False
1  False  False  False   True
2   True   True   True   True

a == d
Out[47]: 
       0      1      2      3
0  False  False  False  False
1  False  False   True  False
2  False  False  False  False
a = pd.DataFrame(np.arange(12).reshape(3,4))

a
Out[49]: 
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

c = pd.Series(np.arange(4))

c
Out[51]: 
0    0
1    1
2    2
3    3
dtype: int32

a > c #不同维度,广播运算,默认在1轴
Out[52]: 
       0      1      2      3
0  False  False  False  False
1   True   True   True   True
2   True   True   True   True

c > 0
Out[53]: 
0    False
1     True
2     True
3     True
dtype: bool

本文内容参考:
中国大学慕课北京理工大学嵩天老师所讲的Python数据分析与展示

如有错误或者不足之处,欢迎大家留言指正!

本文地址:https://blog.csdn.net/qq_45152498/article/details/107191774

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网