Pandas学习
pandas是一个非常高效的处理数据的python库
他是由DataFrame数组来存储多维数组,而多维数组每一列都是一个Series对象。
文章目录
Pandas导入方法
安装
pip3 install pandas
导入
import pandas as pd
Series对象
如果DataFrame对象是二维数组的话,Series就是存储在pandas中的每一列
Series数组对象特点类似于数组和字典的集合体
Series的创建方法
当我们传入列表初始化series对象时,默认初始化index为从0开始的索引
我们也可以给他指定索引
# 传入列表初始化Series对象
# 而打印出来的第一列为索引列,第二列为值,同时Series数组中每个元素的类型都是一样的int64
In [230]: pd.Series([2,3,4])
Out[230]:
0 2
1 3
2 4
dtype: int64
# 初始化Series对象同时给他赋予index索引
# 这时,第一列a,b,c就是数组的索引,2,3,4是数组的值
In [231]: pd.Series([2,3,4],index=['a','b','c'])
Out[231]:
a 2
b 3
c 4
dtype: int64
# 使用numpy对象初始化Series
# 初始化0到5一共5个数字组成的数组
In [232]: pd.Series(np.arange(5))
Out[232]:
0 0
1 1
2 2
3 3
4 4
dtype: int64
# 可以用python字典类型的对象初始化一个series对象
In [9]: sr2 = pd.Series({'a':1,'b':2})
访问Series元素方法
我们可以使用数组下标和index索引两种方式取访问series元素
In [233]: sr = pd.Series([2,3,4],index=['a','b','c'])
In [234]: sr
Out[234]:
a 2
b 3
c 4
dtype: int64
# 使用下标访问
In [235]: sr[0]
<ipython-input-235-e0a22866fe76>:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
sr[0]
Out[235]: 2
# 使用索引访问
In [236]: sr['a']
Out[236]: 2
通常,不推荐直接使用中括号访问Series数组中的值,如果索引也为数组时,使用下标和索引访问可能会有歧义
所以通常会使用下面两种方法访问数组的元素
sr.loc[‘a’] # 使用索引访问
sr.iloc[2] # 使用下标访问
In [237]: sr.loc['a']
Out[237]: 2
In [239]: sr.iloc[0]
Out[239]: 2
如果我们想同时取多个值时,每个索引需要用逗号隔开
# 注意我们需要再加一层中括号,代表'a','b'在同一个维度
# 因为直接sr.loc['a','b']代表取sr的'a'行'b'列,而series数组是一维数组的这样取会直接报错
# sr.loc[['a','b']]取代表取'a','b'两行,同时这两行的所有列都取出来
In [18]: sr.loc[['a','b']]
Out[18]:
a 2
b 3
dtype: int64
与numpy数组类似,series也可以使用bool索引
In [245]: sr = pd.Series([2,3,4],index=['a','b','c'])
In [246]: sr
Out[246]:
a 2
b 3
c 4
dtype: int64
# 获取sr数组中值大于3的元素;
In [247]: sr[sr > 3]
Out[247]:
c 4
dtype: int64
# sr > 3 会返回一个bool数组,满足值>3的元素值为True,否则为False
# sr[sr > 3] 就会显示True的值
In [248]: sr > 3
Out[248]:
a False
b False
c True
dtype: bool
Series切片
series切片类似于numpy对象的切片操作
# 获取下标从1开始到结尾的元素
In [250]: sr.iloc[1:]
Out[250]:
b 3
c 4
dtype: int64
# 按下标查找时区间是前闭后开,如下[1,2) 之间的元素,包括1但不包括下标为2的元素
In [254]: sr.iloc[1:2]
Out[254]:
b 3
dtype: int64
# 获取索引从开头到'b'之间的元素
In [251]: sr.loc[:'b']
Out[251]:
a 2
b 3
dtype: int64
# 按索引查找是区间前后均闭合,如下是['a','b']之间的元素,包括'a'元素和'b'元素
In [255]: sr.loc['a':'b']
Out[255]:
a 2
b 3
dtype: int64
# 切片切出来的数组,索引不会从0开始,但是我们可以通过下标从0开始进行访问
In [23]: sr2 = pd.Series(np.arange(10))
# 这里sr3的索引是5,6,7,8,9.但是我们按照下标访问依然是从0开始数
In [24]: sr3 = sr2.iloc[5:]
In [25]: sr3
Out[25]:
5 5
6 6
7 7
8 8
9 9
dtype: int64
# 按下标访问0号元素
In [29]: sr3.iloc[0]
Out[29]: 5
# 按索引访问索引为5的元素
In [30]: sr3.loc[5]
Out[30]: 5
Series函数
Series所有操作都不会修改原series数据,而且Series支持numpy通用函数
numpy中abs,sqrt等这些都可以使用
import numpy as np
# 直接调用numpy函数即可,注意开方会导致值变为浮点数,故series数组类型变为float64格式
In [257]: np.sqrt(sr)
Out[257]:
a 1.414214
b 1.732051
c 2.000000
dtype: float64
series对象支持与数字或着series对象进行运算
# sr每一个元素都加2
In [4]: sr + 2
Out[4]:
a 4
b 5
c 6
dtype: int64
# sr每一个元素与sr每一个元素相加,索引相同的元素相加。
In [5]: sr + sr
Out[5]:
a 4
b 6
c 8
dtype: int64
与series对象相加时会自动数据对齐,相同索引的值会加在一起,如果找不到对应的索引,会返回NaN
In [32]: sr1 = pd.Series([1,2,3,4],index=['a','b','c','d'])
In [33]: sr2 = pd.Series([6,7,8,9],index=['d','b','c','e'])
In [34]: sr1
Out[34]:
a 1
b 2
c 3
d