好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Data Science | Pandas基础(一)

前文导读

Data Science | Numpy基础(一)

Data Science | Numpy基础(二)

Data Science | 福利列表 | Numpy基础(三)

Pandas是什么?

Pandas是数据分析的核心工具包,基于Numpy创建,为数据分析而存在。

一维数组Series + 二维数组Dataframe

可直接读取数据并做处理(高效简单)

兼容各种数据库

支持各种分析方法

Pandas基本数据结构-Series的基本概念

先举个栗子:

 ar = np.random.rand(5) s = pd.Series(ar) print(ar) print(s,type(s)) >>> [0.1206055  0.83147658 0.88649587 0.2162775  0.31466148] 0    0.120605 1    0.831477 2    0.886496 3    0.216277 4    0.314661 dtype: float64 <class 'pandas.core.series.Series'>  

在这里可以看到这里的Series相比与之前学习的ndarray是一个自带索引index的数组 = 一维的数组 + 对应的索引,当pd.Series单单只看values时就是一个ndarray。

在具体操作方面,Series和ndarray基本相似,包括索引切片的操作差别并不大。

Pandas基本数据结构-Series的创建方法 字典创建Series
 # 字典创建Series dic = {'a':1, 'b':2, 'c':3} s = pd.Series(dic) print(s)  
数组创建Series
 # 数组创建Series arr = np.random.rand(5)*200 s = pd.Series(arr) print(arr) print(s,type(s))  
Series的参数设置

我们可以通过指定Series的index以及dtype参数创建符合我们要求的Series。

 # Series的参数设置 s = pd.Series(arr,index=list('abcde'),dtype=np.int,name=test) print(s) # index参数:设置index,长度保持一致 # dtype参数:设置数值类型 # 那么参数:设置名称  
通过标量创建Series
 s = pd.Series(arr,index=np.arange(5)) print(s)  
Pandas基本数据结构-Series的索引 位置下标索引

位置下标从0开始,索引结果为numpy.float格式并且可以通过float()格式转换为float格式,且位置下标索引是没有负数的。

 s = pd.Series(np.random.rand(5)) print(s) print(s[0],type(s[0]),s[0].dtype) print(float(s[0]),type(float(s[0]))) >>> 0    0.495361 1    0.152195 2    0.217591 3    0.748394 4    0.093389 dtype: float64 0.49536125725281055 <class 'numpy.float64'> float64 0.49536125725281055 <class 'float'>  
标签索引
 # 标签索引 s = pd.Series(np.random.rand(5), index = ['a','b','c','d','e']) print(s) print(s['a'],type(s['a']),s['a'].dtype)  # 如果需要选择多个标签的值,用[[]]来表示(相当于[]中包含一个列表) # 多标签索引结果是新的数组 sci = s[['a','b','e']] print(sci,type(sci)) >>> a    0.714630 b    0.213957 c    0.172188 d    0.972158 e    0.875175 dtype: float64 0.714630383451 <class 'numpy.float64'> float64 a    0.714630 b    0.213957 e    0.875175 dtype: float64 <class 'pandas.core.series.Series'>  
切片索引
 s1 = pd.Series(np.random.rand(5)) s2 = pd.Series(np.random.rand(5), index = ['a','b','c','d','e']) print(s1[1:4],s1[4]) print(s2['a':'c'],s2['c']) print(s2[0:3],s2[3]) print('-----') # 注意:用index做切片是末端包含  print(s2[:-1]) print(s2[::2]) # 下标索引做切片,和list写法一样 >>> 1    0.865967 2    0.114500 3    0.369301 dtype: float64 0.411702342342 a    0.717378 b    0.642561 c    0.391091 dtype: float64 0.39109096261 a    0.717378 b    0.642561 c    0.391091 dtype: float64 0.998978363818 ----- a    0.717378 b    0.642561 c    0.391091 d    0.998978 dtype: float64 a    0.717378 c    0.391091 e    0.957639 dtype: float64  
布尔型索引
 # 布尔型索引 # 数组做判断之后,返回的是一个由布尔值组成的新的数组 # .isnull() / .notnull() 判断是否为空值 (None代表空值,NaN代表有问题的数值,两个都会识别为空值) # 布尔型索引方法:用[判断条件]表示,其中判断条件可以是 一个语句,或者是 一个布尔型数组! s = pd.Series(np.random.rand(3)*100) s[4] = None  # 添加一个空值 print(s) bs1 = s > 50 bs2 = s.isnull() bs3 = s.notnull() print(bs1, type(bs1), bs1.dtype) print(bs2, type(bs2), bs2.dtype) print(bs3, type(bs3), bs3.dtype) print('-----') print(s[s > 50]) print(s[bs3]) >>> 0    2.03802 1    40.3989 2    25.2001 4       None dtype: object 0    False 1    False 2    False 4    False dtype: bool <class 'pandas.core.series.Series'> bool 0    False 1    False 2    False 4     True dtype: bool <class 'pandas.core.series.Series'> bool 0     True 1     True 2     True 4    False dtype: bool <class 'pandas.core.series.Series'> bool ----- Series([], dtype: object) 0    2.03802 1    40.3989 2    25.2001 dtype: object  
巩固练习

分别由字典、数组的方式,创建以下要求的Series

创建一个Series,包含10个元素,且每个值为0-100的均匀分布随机值,index为a-j,请分别筛选出:

标签为b,c的值为多少

Series中第4到6个值是哪些?

Series中大于50的值有哪些?

回复 「作业」 获取练习答案

写在后面

最近咸鱼工作和生活上有些琐事比较忙,所以消失了几天,这两天换完房子就会恢复正常更新。

很多朋友看完上一篇的公众号福利后询问福利的领取方式,详情可以看下本文的置顶留言,资源虽多可不要贪多哦。

咸鱼普拉思

一只咸鱼在编程路上的摸爬滚打,记录摸索中的点点滴滴。

查看更多关于Data Science | Pandas基础(一)的详细内容...

  阅读:32次