使用python进行数据分析时,经常会用Pandas类库处理数据,将数据转换成我们需要的格式。Pandas中的有两个数据结构和处理数据相关,分别是Series和DataFrame。

Series

Series是一种类似于一维数组的对象,它有两个属性,value和index索引。可以像数组那样通过索引访问对应的值,它和数组有点类似也是python中的dict有点类似,数组中的索引只能是数字,而Series的索引既可以是数字类型也可以是字符类型。

创建Series对象

最简单的方式是通过list序列就可以创建Series对象

s1 = Series(['a','b','c','d'])
s1
Out[16]:
0 a
1 b
2 c
3 d

没有指定索引时,会默认生成一个从0开始到N-1的整型索引。

Series会根据传入的list序列中元素的类型判断Series对象的数据类型,如果全部都是整型,则创建的Series对象是整型,如果有一个元素是浮点型,则创建的Series对象是浮点型,如果有一个是字符串,则创建的Series对象是object类型。

s1 = Series([1,2,3,4])
s1
Out[23]:
0 1
1 2
2 3
3 4
dtype: int64
s2 = Series([1,2,3,4.0])
s2
Out[25]:
0 1.0
1 2.0
2 3.0
3 4.0
dtype: float64
s3 = Series([1,2,3,'4'])
s3
Out[27]:
0 1
1 2
2 3
3 4
dtype: object

除了通过list序列创建Series对象外,还可以通过dict创建Series对象。

s1 = Series({'a':1,'b':2,'c':3,'d':4})
s1
Out[37]:
a 1
b 2
c 3
d 4
dtype: int64

通过dict词典创建Series对象时,会将词典的键初始化Series的Index,而dict的value初始化Series的value。

Series还支持传入一个dict词典和一个list序列创建Series对象:

dict1 = {'a':1,'b':2,'c':3,'d':4}
index1 = ['a','b','e']
s1 = Series(dict1,index=index1)
s1
Out[51]:
a 1.0
b 2.0
e NaN
dtype: float64

上面的代码中,指定了创建的Series对象s1的索引是index1,即'a','b'和'e'。s1的值是dict1中和index1索引相匹配的值,如果不匹配,则显示NaN。例如索引'e'和dict1中的键没有相匹配的,则索引'e'的值为NaN。索引'a'和索引'b'都匹配得上,因此值为1和2。

Series通过索引访问值:

s1 = Series({'a':1,'b':2,'c':3,'d':4})
s1
Out[39]:
a 1
b 2
c 3
d 4
dtype: int64
s1['b']
Out[40]: 2

上面代码中通过s1['b']就可以访问到索引b对应的值。

Series支持逻辑和数学运算:

s1 = Series([2,5,-10,200])
s1 * 2
Out[53]:
0 4
1 10
2 -20
3 400
dtype: int64
s1[s1>0]
Out[54]:
0 2
1 5
3 200
dtype: int64

对Series变量做数学运算,会作用于Series对象中的每一个元素。

s1 = Series([2,5,-10,200])
s1[s1>0]
Out[7]:
0 2
1 5
3 200
dtype: int64

对Series做逻辑运算时,会将Series中的值替换为bool类型的对象。

s1 = Series([2,5,-10,200])
s1
Out[10]:
0 2
1 5
2 -10
3 200
dtype: int64
s1 > 0
Out[11]:
0 True
1 True
2 False
3 True
dtype: bool

通过series的逻辑运算,可以过滤掉一些不符合条件的数据,例如过滤掉上面例子中小于0的元素:

s1 = Series([2,5,-10,200])
s1[s1 >0]
Out[23]:
0 2
1 5
3 200
dtype: int64

Series对象和索引都有一个name属性,通过下面的方法可以设置Series对象和索引的name值:

fruit = {0:'apple',1:'orange',2:'banana'}
fruitSeries = Series(fruit)
fruitSeries.name='Fruit'
fruitSeries
Out[27]:
0 apple
1 orange
2 banana
Name: Fruit, dtype: object
fruitSeries.index.name='Fruit Index'
fruitSeries
Out[29]:
Fruit Index
0 apple
1 orange
2 banana
Name: Fruit, dtype: object

可以通过index复制方式直接修改Series对象的index:

fruitSeries.index=['a','b','c']
fruitSeries
Out[31]:
a apple
b orange
c banana
Name: Fruit, dtype: object

DataFrame

DataFrame是表格型的数据结构,和关系型数据库中的表很像,都是行和列组成,有列名,索引等属性。

我们可以认为DataFrame中的列其实就是上面提到的Series,有多少列就有多少个Series对象,它们共享同一个索引index。

通过dict字典创建DataFrame对象:

data = {'fruit':['Apple','Apple','Orange','Orange','Banana'],
'year':[2010,2011,2012,2011,2012],
'sale':[15000,17000,36000,24000,29000]}
frame = DataFrame(data)
frame
Out[12]:
fruit year sale
0 Apple 2010 15000
1 Apple 2011 17000
2 Orange 2012 36000
3 Orange 2011 24000
4 Banana 2012 29000

使用上面的方式创建DataFrame对象时,字典中每个元素的value值必须是列表,并且长度必须一致,如果长度不一致会报错。例如key为fruit、year、sale对应的列表长度必须一致。

创建DataFrame对象和会创建Series对象一样自动加上索引。

通过传入columns参数指定列的顺序:

data = {'fruit':['Apple','Apple','Orange','Orange','Banana'],
'year':[2010,2011,2012,2011,2012],
'sale':[15000,17000,36000,24000,29000]}
frame = DataFrame(data,columns=['sale','fruit','year','price'])
frame
Out[25]:
sale fruit year price
0 15000 Apple 2010 NaN
1 17000 Apple 2011 NaN
2 36000 Orange 2012 NaN
3 24000 Orange 2011 NaN
4 29000 Banana 2012 NaN

如果传入的列在数据中找不到,就会产生NaN值。

DataFrame的index也是可以修改的,同样传入一个列表:

frame = DataFrame(data,columns=['sale','fruit','year'],index=[4,3,2,1,0])
frame
Out[22]:
sale fruit year
4 15000 Apple 2010
3 17000 Apple 2011
2 36000 Orange 2012
1 24000 Orange 2011
0 29000 Banana 2012

通过传入的[4,3,2,1,0]就将原来的index从0,1,2,3,4改变为4,3,2,1,0。

通过DataFrame对象获取Series对象:

frame['year']
Out[26]:
0 2010
1 2011
2 2012
3 2011
4 2012
Name: year, dtype: int64
frame['fruit']
Out[27]:
0 Apple
1 Apple
2 Orange
3 Orange
4 Banana
Name: fruit, dtype: object

frame['fruit']和frame.fruit都可以获取列,并且返回的是Series对象。

DataFrame赋值,就是对列赋值,首先获取DataFrame对象中某列的Series对象,然后通过赋值的方式就可以修改列的值:

data = {'fruit':['Apple','Apple','Orange','Orange','Banana'],
'year':[2010,2011,2012,2011,2012],
'sale':[15000,17000,36000,24000,29000]}
frame = DataFrame(data,columns=['sale','fruit','year','price'])
frame
Out[24]:
sale fruit year price
0 15000 Apple 2010 NaN
1 17000 Apple 2011 NaN
2 36000 Orange 2012 NaN
3 24000 Orange 2011 NaN
4 29000 Banana 2012 NaN
frame['price'] = 20
frame
Out[26]:
sale fruit year price
0 15000 Apple 2010 20
1 17000 Apple 2011 20
2 36000 Orange 2012 20
3 24000 Orange 2011 20
4 29000 Banana 2012 20
frame.price = 40
frame
Out[28]:
sale fruit year price
0 15000 Apple 2010 40
1 17000 Apple 2011 40
2 36000 Orange 2012 40
3 24000 Orange 2011 40
4 29000 Banana 2012 40
frame.price=np.arange(5)
frame
Out[30]:
sale fruit year price
0 15000 Apple 2010 0
1 17000 Apple 2011 1
2 36000 Orange 2012 2
3 24000 Orange 2011 3
4 29000 Banana 2012 4

通过frame['price']或者frame.price获取price列,然后通过frame['price']=20或frame.price=20就可以将price列都赋值为20。

也可以通过numpy的arange方法进行赋值。如上面的代码所示。

可以通过Series给DataFrame对象赋值:

data = {'fruit':['Apple','Apple','Orange','Orange','Banana'],
'year':[2010,2011,2012,2011,2012],
'sale':[15000,17000,36000,24000,29000]}
frame = DataFrame(data,columns=['sale','fruit','year','price'])
frame
Out[6]:
sale fruit year price
0 15000 Apple 2010 NaN
1 17000 Apple 2011 NaN
2 36000 Orange 2012 NaN
3 24000 Orange 2011 NaN
4 29000 Banana 2012 NaN
priceSeries = Series([3.4,4.2,2.4],index = [1,2,4])
frame.price = priceSeries
frame
Out[9]:
sale fruit year price
0 15000 Apple 2010 NaN
1 17000 Apple 2011 3.4
2 36000 Orange 2012 4.2
3 24000 Orange 2011 NaN
4 29000 Banana 2012 2.4

这种赋值方式,DataFrame的索引会和Series的索引自动匹配,在对应的索引位置赋值,匹配不上的位置将填上缺失值NaN。

创建的Series对象如果不指定索引时的赋值结果:

priceSeries = Series([3.4,4.2,2.4])
frame.price = priceSeries
frame
Out[12]:
sale fruit year price
0 15000 Apple 2010 3.4
1 17000 Apple 2011 4.2
2 36000 Orange 2012 2.4
3 24000 Orange 2011 NaN
4 29000 Banana 2012 NaN

DataFrame还支持通过列表或者数组的方式给列赋值,但是必须保证两者的长度一致:

priceList=[3.4,2.4,4.6,3.8,7.3]
frame.price=priceList
frame
Out[15]:
sale fruit year price
0 15000 Apple 2010 3.4
1 17000 Apple 2011 2.4
2 36000 Orange 2012 4.6
3 24000 Orange 2011 3.8
4 29000 Banana 2012 7.3
priceList=[3.4,2.4,4.6,3.8,7.3]
frame.price=priceList

赋值的列如果不存在时,相当于创建出一个新列:

frame['total'] = 30000
frame
Out[45]:
sale fruit year price total
0 15000 Apple 2010 3.4 30000
1 17000 Apple 2011 2.4 30000
2 36000 Orange 2012 4.6 30000
3 24000 Orange 2011 3.8 30000
4 29000 Banana 2012 7.3 30000

上面的例子通过给不存在的列赋值,新增了新列total。必须使用frame['total']的方式赋值,不建议使用frame.total,使用frame.的方式给不存在的列赋值时,这个列会隐藏起来,直接输出DataFrame对象是不会看到这个total这个列的,但是它又真实的存在,下面的代码是分别使用frame['total']和frame.total给frame对象的total列赋值,total列开始是不存在的:

frame
Out[60]:
sale fruit year price
0 15000 Apple 2010 3.4
1 17000 Apple 2011 2.4
2 36000 Orange 2012 4.6
3 24000 Orange 2011 3.8
4 29000 Banana 2012 7.3
frame.total = 20
frame
Out[62]:
sale fruit year price
0 15000 Apple 2010 3.4
1 17000 Apple 2011 2.4
2 36000 Orange 2012 4.6
3 24000 Orange 2011 3.8
4 29000 Banana 2012 7.3
frame['total'] = 20
frame
Out[64]:
sale fruit year price total
0 15000 Apple 2010 3.4 20
1 17000 Apple 2011 2.4 20
2 36000 Orange 2012 4.6 20
3 24000 Orange 2011 3.8 20
4 29000 Banana 2012 7.3 20

使用frame.total方式赋值时,是看不到total这一列的,而用frame['total']方式赋值时,则可以看到total这一列。

数据分析入门——Pandas类库基础知识的更多相关文章

  1. 数据分析入门——numpy类库基础知识

    numpy类库是数据分析的利器,用于高性能的科学计算和数据分析.使用python进行数据分析,numpy这个类库是必须掌握的.numpy并没有提供强大的数据分析功能,而是它提供的ndarray数据结构 ...

  2. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  3. Hibernate入门1. Hibernate基础知识入门

    Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...

  4. Python 入门之Python基础知识

    Python 入门之Python基础知识 1.变量 (1)变量就是把程序运行的中间结果临时存在内存中,以便后续代码使用 (2)变量的作用: 昵称,就是代指内存中某个地址中的内容 a = 123 变量名 ...

  5. JAVAWeb入门之JSP基础知识

    也是到了考试周,很多课都结了,准备去学点新东西.随后就开始自学JAVAWeb. 要学习JAVAWeb,首先需下面的知识: a)      HTML/CSS/JS(前端页面),XML,JSON,vue ...

  6. 数据分析入门——pandas之DataFrame基本概念

    一.介绍 数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列. 可以看作是Series的二维拓展,但是df有行列索引:index.column 推荐参考:https://www. ...

  7. 数据分析入门——pandas之Series

    一.介绍 Pandas是一个开源的,BSD许可的库(基于numpy),为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 官方中文文档:https://www.pypandas.cn ...

  8. Kinect for Windows SDK开发入门(二):基础知识 上

    原文来自:http://www.cnblogs.com/yangecnu/archive/2012/03/31/KinectSDK_Application_Fundamentals_Part1.htm ...

  9. ios网络编程(入门级别)-- 基础知识

    在学习ios的过程中,停留在UI控件很长时间,现在正在逐步的接触当中!!!!!!在这个过程中,小编学到了一些关于网络编程知识,并且有感而发,在此分享一下: 关于网络请求的重要性我想不用多说了吧!!!对 ...

随机推荐

  1. HDU4899 Hero meet devil DP套DP

    陈老师的题QwQ 原题链接 题目大意 有两个字符串\(S\)和\(T\)(都只能由'A','C','G','T'这四个字符组成),\(S\)已知\(T\)未知,还知道\(S\)的长度为\(m\).求满 ...

  2. 20175209 《Java程序设计》第九周学习总结

    20175209 <Java程序设计>第九周学习总结 一.教材知识点总结 有关数据库下载中存在可能出现的问题已经在博客<数据库安装和使用过程中出现的一些问题>给出了相应的解决办 ...

  3. 获取windows凭证管理器明文密码

    1.运行cmdkey /list查看windows保存凭证 方法1.mimikaz mimikatz vault::cred 2.利用powershell尝试获取 windows 普通凭据类型中的明文 ...

  4. webstorm开发微信小程序

    参考博客:https://www.cnblogs.com/pansidong/articles/7563155.html

  5. localStorage sessionStorage cookie indexedDB

    目录: localStorage sessionStorage cookie indexedDB localStorage localStorage存储的数据能在跨浏览器会话保留 数据可以长期保留,关 ...

  6. python之路(6)迭代器和生成器

     目录 迭代器(Iterator) 生成器(Generator) 迭代器 迭代器协议:对象提供一个next方法,执行该方法要么返回下一项,要么引起一个Stopiteration异常 可迭代对象:实现了 ...

  7. SQL 农经权数据库问题提取_身份证号码相同(字段值出现多次);身份证号码相同但姓名不同(A字段相同,B字段不相同);发包方无承包方信息(A表有,B表无)等

    身份证号码相同(字段值出现多次) select * from CBF_JTCY a,(select CYZJHM, count(*) from CBF_JTCY  group by  CYZJHM h ...

  8. GCC 警告

    -w -W禁止/开启 编译警告的打印.这个警告不建议使用.大约2012年底,公司代码进行一次大重构,另外从Codeblock集成开发环境转向Makefile管理,Makefile里面默认使用了-w,因 ...

  9. react native 安卓home返回键页面刷新

    import { withNavigationFocus } from 'react-navigation'; class Warngreete extends React.Component { c ...

  10. Storage 002 电商数据库设计

    [用户模块] 账户登录:邮箱/用户名/已验证手机 密码 如果将所有字段都放到一张表里存储? 数据插入异常        只想插入一个值的  由于需要主键信息,插入的一行变成新的一行,和原来的记录无关. ...