pandas模块篇(之二)
阅读原文时间:2023年07月09日阅读:1

今日内容概要

  • 布尔选择器
  • 索引
  • 数据对齐
  • 数据操作(增出改查)
  • 算术方法
  • DataFrame(Excel表格数据)

布尔选择器

import numpy as np
import pandas as pd
res = pd.Series([True,False,False,True,False])
price = pd.Series([321321,123,324,5654,645])

# 掌握
price[res]
0    321321
3      5654
dtype: int64

# 了解
price|res
0    True
1    True
2    True
3    True
4    True
dtype: bool

price&res
0     True
1    False
2    False
3    False
4    False
dtype: bool

# 需要掌握
(price > 100) & (price < 700)
0    False
1     True
2     True
3    False
4     True
dtype: bool

price[(price > 100) & (price < 700)]
1    123
2    324
4    645
dtype: int64

索引及标签

res1 = pd.Series({'a':111,'b':222,'c':333,'d':444,'e':555})
res1
a    111
b    222
c    333
d    444
e    555
dtype: int64
# 索引取值
res1[0]
111
# 标签取值
res1['a']
111

# 获取所有的标签
res1.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

# 给标签加列名称
res1.index.name = 'STA'
res1
STA
a    111
b    222
c    333
d    444
e    555
dtype: int64

# data_range时间间隔
res2 = pd.date_range('2020-01-01','2020-12-01',freq='M')  # frep后面按照指定的时间间隔(年'Y',月'M',日'D')
res2
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30',
               '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31',
               '2020-09-30', '2020-10-31', '2020-11-30'],
              dtype='datetime64[ns]', freq='M')
# 还可以将日期作为Series的标签
res3 = pd.Series([111,222,333,444,555],index=res3)
res3
2020-01-31    111
2020-02-29    222
2020-03-31    333
2020-04-30    444
2020-05-31    555
Freq: M, dtype: int64

res3.index.name = '日期'
日期
2020-01-31    111
2020-02-29    222
2020-03-31    333
2020-04-30    444
2020-05-31    555
Freq: M, dtype: int64

整数索引

1 整数索引
x1 = pd.Series(np.arange(11))
x1
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10
dtype: int32
x2 = x1[4:]
x2
4      4
5      5
6      6
7      7
8      8
9      9
10    10
dtype: int32

##################################################################################################
# 索引取值
# x1[1] # 报错
'''针对取值操作,以后需要用特定方法来约束'''
# iloc按照索引的方式取值
# loc按照标签的方式取值
# x1.iloc[1] # 1
x1.loc[3]  # 3
'''非常重要,一定要记忆'''
###################################################################################################

数据对齐

a1 = pd.Series([12,23,34,45],index=['c','a','d','b'])
a2 = pd.Series([11,20,10,30],index=['d','c','a','b'])
a1 + a2
运行结果:
a    33
b    75
c    32
d    45
dtype: int64

# 可以通过这种索引对齐直接将两个Series对象进行运算
a3 = pd.Series([11,20,10,14],index=['d','c','a','e'])
a1 + a3
运行结果:
a    33.0
b     NaN
c    32.0
d    45.0
e     NaN
dtype: float64
# a1和a3的索引不一致,所以最终的运行会发现e索引对应的值无法运算,就返回了NAN,一个缺失值

'''
疑问:为什么运算完之后数据类型会由原来的int64变成float64?
因为NaN其实是float类型
type(np.nan)
结果是:float
'''

数据操作

'''增删改查'''
a3= pd.Series([11,20,10,14],index=['d','c','a','e'])
a3
d    11
c    20
a    10
e    14
dtype: int64
# 查
a3.loc['a']
10

# 改
a3.iloc[2]= 100
a3
d     11
c     20
a    100
e     14
dtype: int64

# 增
# 方式1:append不修改原数据
a3.append(pd.Series([66],index=['e']))
d     11
c     20
a    100
e     14
e     66
dtype: int64
#方式2:set_value直接修改原数据
a3.set_value('f',999)  # 会有一个提示 如果不想有这个提示需要配置
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead
d     11
c     20
a    100
e     14
f    999
dtype: int64
a3
d     11
c     20
a    100
e     14
f    999
dtype: int64

# 删: del关键字作用的也是原数据
del a3['f']
a3
d     11
c     20
a    100
e     14
dtype: int64

灵活的算术方法

"""
针对加减乘除等数学运算
可以直接使用符号
也可以使用提供的方法名(可以有额外的功能)
add
sub
div
mul
"""
b1 = pd.Series([12,23,34], index=['c','a','b'])
b3 = pd.Series([11,20,10,14], index=['d','c','a','b'])
b1
c    12
a    23
b    34
dtype: int64
b3
d    11
c    20
a    10
b    14
dtype: int64
tes = b1 + b3
tes
a    33.0
b    48.0
c    32.0
d     NaN
dtype: float64
tes1 = b1*b3
tes1
a    230.0
b    476.0
c    240.0
d      NaN
dtype: float64
b1.add(b3,fill_value=666)
b1
c    12
a    23
b    34
dtype: int64
b3
d    11
c    20
a    10
b    14
dtype: int64
fill_value
b1.add(b3,fill_value=0) # 在运行之前找出调用该方法的Series当中的缺失值补全后再运算
a    33.0
b    48.0
c    32.0
d    11.0
dtype: float64
b1.mul(b3,fill_value=1)
a    230.0
b    476.0
c    240.0
d     11.0
dtype: float64

DataFrame

表格型数据结构,相当于一个二维数组,含有一组有序的列也可以看作是由Series组成

基本使用

# 创建Dataframe有很多中方式,但是一般情况下我们都不需要自己创建DataFrame而是将excel文件直接引导成DataFrame

# 方式1 传字典字典的键会变成表格的列名称 行名称默认是索引
import numpy as np
import pandas as pd
res = pd.DataFrame({'one':[1,2,3,4],'two':[4,3,2,1]})
res
one    two
0    1   4
1    2   3
2    3   2
3    4   1

# 取值
res['one']  # 默认是Series的展示形式
0    1
1    2
2    3
3    4
Name: one, dtype: int64
res[['two']] # 再加[]就会变成表格的形式
two
0    4
1    3
2    2
3    1
res['two'][1] # 第一个中括号里面是列 第二个中括号里面是行
3

# 方式2: 直接传Series 如果Series有自定义的标签 那么生成的DataFrame列名称采用的就是标签名
res1 = pd.DataFrame({'one':pd.Series([1,2,3],index=['c','b','a']),'two':pd.Series([1,2,3],index=['b','a','c'])})
res1
one    two
a    3   2
b    2   1
c    1   3

# 方式3:自定义行列 index行 columns列
pd.DataFrame(np.array([[10,20,30],[40,50,60]]),index=['a','b'],columns=['c1','c2','c3'])
c1    c2  c3
a    10  20  30
b    40  50  60
arange

# 方式4:列表中有几个元素就会生成几行数据  不指定行列默认都是用索引表示
pd.DataFrame([np.arange(1,8),np.arange(11,18),np.arange(21,28)])
0    1   2   3   4   5   6
0    1   2   3   4   5   6   7
1    11  12  13  14  15  16  17
2    21  22  23  24  25  26  27

# 方式5:会自动找行列的对应位置 没有的用NaN表示缺失值
s1 = pd.Series(np.arange(1,9,2))
s2 = pd.Series(np.arange(2,10,2))
s3 = pd.Series(np.arange(5,7),index=[1,2])
s1
0    1
1    3
2    5
3    7
dtype: int32
s2
0    2
1    4
2    6
3    8
dtype: int32
s3
1    5
2    6
dtype: int32
df5 = pd.DataFrame({'c1':s1,'c2':s2,'c3':s3})
df5
c1    c2  c3
0    1   2   NaN
1    3   4   5.0
2    5   6   6.0
3    7   8   NaN
'''以上创建房事后都仅仅做一个了解即可,因为工作在中dataframe的数据一般都是来自于读取外部文件数据'''

常见属性及方法

1.index  行索引
2.columns 列索引
3.T        转置
4. values  值索引
5.describe 快速统计

# index获取行索引
df5.index
Int64Index([0,1,2,3],dtype='int64')
# columns获取列索引
df5.columns
Index(['c1', 'c2', 'c3'], dtype='object')
# T转置 行列互换
df5.T
0    1   2   3
c1    1.0 3.0 5.0 7.0
c2    2.0 4.0 6.0 8.0
c3    NaN 5.0 6.0 NaN
df5
c1    c2  c3
0    1   2   NaN
1    3   4   5.0
2    5   6   6.0
3    7   8   NaN
values
# values获取表格数据 组织成二维数组的形式
df5.values
array([[ 1.,  2., nan],
       [ 3.,  4.,  5.],
       [ 5.,  6.,  6.],
       [ 7.,  8., nan]])
# describe常见的数学统计
df5.describe()
c1    c2  c3
count    4.000000    4.000000    2.000000
mean    4.000000    5.000000    5.500000
std    2.581989    2.581989    0.707107
min    1.000000    2.000000    5.000000
25%    2.500000    3.500000    5.250000
50%    4.000000    5.000000    5.500000
75%    5.500000    6.500000    5.750000
max    7.000000    8.000000    6.000000