pandas之数据结构
- pandas数据结构
- Series
- 创建
- 赋值
- 字典创建
- 数组运算
- DataFrame
- 创建
- 索引
- 列索引
- 行索引
- 列赋值
- 属性
- 索引
pandas数据结构
这里需要谨记两点
pandas是基于numpy构建的。
pandas的数据结构实际上和Excel是一致的。
为什么这样说第二点呢?
因为在我看来,它的数据结构可以这样理解。
Series就相当于Excel的一列,有对应的行号。
DataFrame就相当于Excel表格,有行,有列,每一列还可以是不同的数据类型。
索引对象,就是这些行号,列号,以及Excel中绝对引用与相对引用。
如果按照上面这样想,就很容易理解了。
Series
创建
Series有一组数据(各种numpy数据类型)以及与之相关的数据标签(索引组成)。
就相当于Excel中的一列以及对应的行号。
In [1]: import numpy as npIn [2]: import pandas as pdIn [3]: from pandas import Series,DataFrameIn [4]: ser = Series([-2,-4,3,-1,5])In [5]: ser
Out[5]:
0 -2
1 -4
2 3
3 -1
4 5
dtype: int64
可以看到,Series与Excel的表格中的某列,极其相似,索引在左,值在右。
这里的0,1,...4是Series自动创建的索引。
这也说明,如果我们不指定索引,那么Series会自动为数据值创建一个0-(N-1)
(N为数据长度)的整数型索引。
不过,Series支持我们指定索引,通过index
参数。
In [7]: ser2 = Series([-2,-4,3,-1,5],index=['d','a','c','b','e'])In [8]: ser2
Out[8]:
d -2
a -4
c 3
b -1
e 5
dtype: int64
我们可以通过Series的属性values
和index
来查看数组及其索引。
In [9]: ser2.values#查看数组形式
Out[9]: array([-2, -4, 3, -1, 5], dtype=int64)In [10]: ser2.index#查看索引
Out[10]: Index(['d', 'a', 'c', 'b', 'e'], dtype='object')
赋值
还可通过索引赋值,取值,赋给不存在的索引值会添加一个新值。
In [11]: ser2['c'] #取单值
Out[11]: 3In [12]: ser2['b'] = 12#赋值In [13]: ser2
Out[13]:
d -2
a -4
c 3
b 12
e 5
dtype: int64In [14]: ser2['f'] = 10 #添加新值In [16]: ser2[['c','b','f','a']] #取出一组值
Out[16]:
c 3
b 12
f 10
a -4
dtype: int64
我们还可以复制的方式来更改索引
In [43]: ser.index = ['one','two','three','four','five']In [44]: ser
Out[44]:
one -2
two -4
three 3
four -1
five 5
dtype: int64
字典创建
其实,正常的想法应该是将Series看做一个有序字典,因为很显然通过上面的实验我们可以发现,它也是索引值到数据值的映射。因此它可以用在字典函数中。
In [17]: 'b' in ser2
Out[17]: TrueIn [18]: 'g' in ser2
Out[18]: False
故,可以用字典来创建Series。
In [19]: zidian = {'nihao':2000,'wobuhao':2300,'hahah':3020,'niqu':4300}#字典In [20]: ser3 = Series(zidian)#创建SeriesIn [21]: ser3
Out[21]:
hahah 3020
nihao 2000
niqu 4300
wobuhao 2300
dtype: int64
可以注意到
字典的键就是Series中的索引,而且还会有序排列。
In [29]: suoyin = ['jiushi','nihao','niqu','wobuhao']
In [30]: ser4 = Series(zidian,index = suoyin)
In [31]: ser4
Out[31]:
jiushi NaN
nihao 2000.0
niqu 4300.0
wobuhao 2300.0
dtype: float64
在上面这个实验中可以发现,
我们指定了索引,zidian中与suoyin相匹配的值被取了出来,而jiushi
没有值,匹配不到,因此结果为NaN。
NaN(not a number)非数字,它表示缺失或NA值
缺失(mising)或NA表示缺失数据。
pandas中的isnull
和notnull
函数用于检测缺失数据。Series也有类似的示例方法。
In [32]: pd.isnull(ser4)
Out[32]:
jiushi True
nihao False
niqu False
wobuhao False
dtype: boolIn [33]: pd.notnull(ser4)
Out[33]:
jiushi False
nihao True
niqu True
wobuhao True
dtype: boolIn [34]: ser4.isnull()
Out[34]:
jiushi True
nihao False
niqu False
wobuhao False
dtype: boolIn [35]: ser4.notnull()
Out[35]:
jiushi False
nihao True
niqu True
wobuhao True
dtype: bool
置于缺失数据怎样处理,我们以后在讨论。
数组运算
numpy的数组运算都可以应用到Series。
In [26]: ser[ser > 0] # 布尔数组过滤
Out[26]:
2 3
4 5
dtype: int64In [27]: ser * 2 #标量运算
Out[27]:
0 -4
1 -8
2 6
3 -2
4 10
dtype: int64In [28]: np.exp(ser) # 应用数学函数
Out[28]:
0 0.135335
1 0.018316
2 20.085537
3 0.367879
4 148.413159
dtype: float64In [36]: ser3
Out[36]:
hahah 3020
nihao 2000
niqu 4300
wobuhao 2300
dtype: int64In [37]: ser4
Out[37]:
jiushi NaN
nihao 2000.0
niqu 4300.0
wobuhao 2300.0
dtype: float64In [38]: ser3 + ser4 #两个Series相加
Out[38]:
hahah NaN
jiushi NaN
nihao 4000.0
niqu 8600.0
wobuhao 4600.0
dtype: float64
可以注意到:
索引与值之间的连接一直被保留
值会变,索引不会变
联想下Excel,不论做什么运算,行号是不会变得。
两个Series相加也类似于Excel中的合并计算。
Series对象本身和索引还有一个name属性。
In [39]: ser4.naem = 'test'#name属性In [40]: ser4.index.name = 'number'#name属性In [41]: ser4
Out[41]:
number
jiushi NaN
nihao 2000.0
niqu 4300.0
wobuhao 2300.0
dtype: float64ser2.name = 'test'ser2.index.naem = 'pop'ser2
Out[10]:
d -2
a -4
c 3
b -1
e 5
Name: test, dtype: int64
DataFrame
DataFrame就是一个表个性的数据结构,它基本和Excel是一样的。有行索引(行号),列索引(列号)。
它对行和列的操作基本一致。
也可以看成由Series组成的字典。
创建
- 通过传入等长列表组成的字典。
- 传入numpy数组组成的字典。
In [3]: data = {'date':[2015,2012,2000,2013,2016],'city':['haerbin','beijing','tianjin','shijiazhuang','jinan'],'shuju':[1.2,1.3,1.4,2.3,2.7]}#等长列表字典In [4]: frame = DataFrame(data)In [5]: frame
Out[5]:city date shuju
0 haerbin 2015 1.2
1 beijing 2012 1.3
2 tianjin 2000 1.4
3 shijiazhuang 2013 2.3
4 jinan 2016 2.7In [6]: frame1 = DataFrame(np.random.randn(16).reshape(4,4))#numpy数组In [7]: frame1
Out[7]:0 1 2 3
0 -0.077134 0.983138 -0.483991 -1.107506
1 -1.721202 0.057802 0.090921 0.411377
2 -1.973126 1.335374 -0.524562 0.017366
3 -0.311434 1.819138 -0.258790 -0.663582
可以注意到,DataFrame会像Series一样自动的加上索引,且全部列会被有序排列。
我们也可以指定列的顺序(行也是适用的)。
In [8]: DataFrame(data,columns=['date','city','shuju'])
Out[8]:date city shuju
0 2015 haerbin 1.2
1 2012 beijing 1.3
2 2000 tianjin 1.4
3 2013 shijiazhuang 2.3
4 2016 jinan 2.7
与Series一样,如果,我们指定的列在数据中不存在,也会产生NA值。
这就相当于,有列名,但是没有对应的值。
In [12]: frame2 = DataFrame(data,columns=['date','city','shuju','nohere'],index = ['one','two','three','four','five'])In [13]: frame2
Out[13]:date city shuju nohere
one 2015 haerbin 1.2 NaN
two 2012 beijing 1.3 NaN
three 2000 tianjin 1.4 NaN
four 2013 shijiazhuang 2.3 NaN
five 2016 jinan 2.7 NaNIn [14]: frame2.columns
Out[14]: Index(['date', 'city', 'shuju', 'nohere'], dtype='object')
但是,指定了six行的话就会返回错误,因为数据比我们的行少。
索引
我们可以通过字典或者属性的方式来获得一个Series。
列索引
In [15]: frame2['city'] #字典方式
Out[15]:
one haerbin
two beijing
three tianjin
four shijiazhuang
five jinan
Name: city, dtype: objectIn [16]: frame2.city #属性方式
Out[16]:
one haerbin
two beijing
three tianjin
four shijiazhuang
five jinan
Name: city, dtype: objectIn [27]: frame2[['date','shuju']]#两列
Out[27]:date shuju
one 2015 1.2
two 2012 1.3
three 2000 1.4
four 2013 2.3
five 2016 2.7In [28]: frame2[[0,1,2]]#索引号
Out[28]:date city shuju
one 2015 haerbin 1.2
two 2012 beijing 1.3
three 2000 tianjin 1.4
four 2013 shijiazhuang 2.3
five 2016 jinan 2.7In [29]: frame2[[1:2]]#切片File "<ipython-input-29-756d0bb4bbed>", line 1frame2[[1:2]]^
SyntaxError: invalid syntax
通过以上代码,我们可以知道DataFrame与Series的索引是相同的。这样获取Series的话,其name属性也被设置好了。
另外,发现DataFrame的列也是存在索引号的。
切片的方法也是行不通的,会返回错误
行索引
至于行的获取我们可以通过位置或者名称的方式。如索引字段ix。
还有其他方法,如:
- 类似列表切片,可以看到器索引也是从0开始。
- 按照指定的索引选择一行或多行,使用函数
.loc[]
。 - 指定位置来选择一行或多行,使用
.ioc[]
。
In [18]: frame2.ix['three']
Out[18]:
date 2000
city tianjin
shuju 1.4
nohere NaN
Name: three, dtype: object#此文,不是同一天写的In [6]: frame2[0:2]#类似于列表切片
Out[6]:date city shuju nohere
one 2015 haerbin 1.2 NaN
two 2012 beijing 1.3 NaNIn [12]: frame2[:-2]#类似于列表切片
Out[12]:date city shuju nohere
one 2015 haerbin 1.2 NaN
two 2012 beijing 1.3 NaN
three 2000 tianjin 1.4 NaNIn [13]: frame2[-4:-2]#类似于列表切片
Out[13]:date city shuju nohere
two 2012 beijing 1.3 NaN
three 2000 tianjin 1.4 NaNIn [18]: frame2.loc['one']#.loc来选定一行
Out[18]:
date 2015
city haerbin
shuju 1.2
nohere NaN
Name: one, dtype: objectIn [20]: frame2.loc['one':'three']#.loc选定多行
Out[20]:date city shuju nohere
one 2015 haerbin 1.2 NaN
two 2012 beijing 1.3 NaN
three 2000 tianjin 1.4 NaN
可以发现,行索引是可以进行切片的,列是不可以的,具体的索引方法使用,这里就不仔细研究了,因为这里不是重点。
列赋值
可以直接给列进行赋值,这里需要用到索引。
In [35]: frame2['nowhere'] = 1.5In [36]: frame2
Out[36]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 1.5
two 2012 beijing 1.3 NaN 1.5
three 2000 tianjin 1.4 NaN 1.5
four 2013 shijiazhuang 2.3 NaN 1.5
five 2016 jinan 2.7 NaN 1.5In [37]: frame2.nowhere = 2In [38]: frame2
Out[38]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 2
two 2012 beijing 1.3 NaN 2
three 2000 tianjin 1.4 NaN 2
four 2013 shijiazhuang 2.3 NaN 2
five 2016 jinan 2.7 NaN 2In [39]: frame2['nowhere'] = np.arange(5.0)In [40]: frame2
Out[40]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 0.0
two 2012 beijing 1.3 NaN 1.0
three 2000 tianjin 1.4 NaN 2.0
four 2013 shijiazhuang 2.3 NaN 3.0
five 2016 jinan 2.7 NaN 4.0In [41]: frame2.nowhere = np.arange(5.0,10.0)In [42]: frame2
Out[42]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 5.0
two 2012 beijing 1.3 NaN 6.0
three 2000 tianjin 1.4 NaN 7.0
four 2013 shijiazhuang 2.3 NaN 8.0
five 2016 jinan 2.7 NaN 9.0In [43]: frame2.nowhere = [1,2,3,4,5,6]ValueError: Length of values does not match length of index
将列表或数组赋值给某列时,其长度必须与DataFrame的列的长度匹配,如果将Series赋值给DataFrame,则其会与DataFrame的索引精确匹配,如果有空位,则空位是缺失值。
In [44]: x = Series([12, 1.7, 1.9],index = ['one','four','three'])#SereiesIn [45]: frame2.nowhere = x #将Series赋值给frame2的nowhereIn [46]: frame2
Out[46]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 12.0
two 2012 beijing 1.3 NaN NaN
three 2000 tianjin 1.4 NaN 1.9
four 2013 shijiazhuang 2.3 NaN 1.7
five 2016 jinan 2.7 NaN NaN
为不存在的列赋值,会创建这个不存在的列。del是关键字,他可以用来删除列
In [53]: frame2['wu'] = frame2.city == 'tianjin'In [54]: frame2
Out[54]:date city shuju nohere nowhere wu
one 2015 haerbin 1.2 NaN 12.0 False
two 2012 beijing 1.3 NaN NaN False
three 2000 tianjin 1.4 NaN 1.9 True
four 2013 shijiazhuang 2.3 NaN 1.7 False
five 2016 jinan 2.7 NaN NaN FalseIn [57]: del frame2['wu']#删除列In [58]: frame2
Out[58]:date city shuju nohere nowhere
one 2015 haerbin 1.2 NaN 12.0
two 2012 beijing 1.3 NaN NaN
three 2000 tianjin 1.4 NaN 1.9
four 2013 shijiazhuang 2.3 NaN 1.7
five 2016 jinan 2.7 NaN NaNIn [59]: frame2.columns#查看列
Out[59]: Index(['date', 'city', 'shuju', 'nohere', 'nowhere'], dtype='object')
经过试验,发现在这里使用frame2.wu
的索引方式结果不变,也就是说这种方法在这里行不通,因此,最好使用上方代码块的方式。
我们还需要知道的是,对与索引所返回的Series所做的任何更改都会反映到DataFrame上面。
DataFrame还可以接受嵌套字典,也就是字典的字典。外层字典的键作为列,内层键则作为行的索引。
属性
我们可以设置DataFrame的index和columns属性,也就是行和列的属性。
In [60]: frame2.index.name = 'year'In [61]: frame2.columns.name = 'state'In [62]: frame2
Out[62]:
state date city shuju nohere nowhere
year
one 2015 haerbin 1.2 NaN 12.0
two 2012 beijing 1.3 NaN NaN
three 2000 tianjin 1.4 NaN 1.9
four 2013 shijiazhuang 2.3 NaN 1.7
five 2016 jinan 2.7 NaN NaN
DataFrame的values属性与Series一样,都会以二维数组形式返回。
In [63]: frame2.values
Out[63]:
array([[2015, 'haerbin', 1.2, nan, 12.0],[2012, 'beijing', 1.3, nan, nan],[2000, 'tianjin', 1.4, nan, 1.9],[2013, 'shijiazhuang', 2.3, nan, 1.7],[2016, 'jinan', 2.7, nan, nan]], dtype=object)
可以看到dtype的类型是object,这是因为其各列的数据类型不同,因此返回了一个可以兼容所有列的数值类型。
索引
上方我们已经说了很久的索引对象,索引对象最大的特点是不可修改性(immutable)。
In [64]: inde = frame2.indexIn [65]: inde
Out[65]: Index(['one', 'two', 'three', 'four', 'five'], dtype='object', name='year')In [66]: inde[1]
Out[66]: 'two'In [67]: inde[1] = 'hah'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-67-79c2a592e832> in <module>()
----> 1 inde[1] = 'hah'D:\Users\sunshine\Anaconda\lib\site-packages\pandas\indexes\base.py in __setitem__(self, key, value)14021403 def __setitem__(self, key, value):
-> 1404 raise TypeError("Index does not support mutable operations")14051406 def __getitem__(self, key):TypeError: Index does not support mutable operations
判断某一行或者某一列是否在索引中。
In [70]: 'city' in frame2.columns
Out[70]: TrueIn [71]: 'three' in frame2.index
Out[71]: True
除此之外,index还存在很多方法和属性。
索引方法和属性 | 说明 |
---|---|
append | 链接额外的索引对象,产生一个新的索引 |
diff | 计算索引的差集 |
intersection | 计算交集 |
union | 计算并集 |
isin | 计算出一个布尔数组表示每一个值是否包含在所传递的集合里 |
delete | 计算删除位置i的元素的索引 |
drop | 计算删除所传递的值后的索引 |
insert | 计算在位置i插入元素后的索引 |
is_monotonic | 返回True,如果每一个元素都比它前面的元素大或相等 |
is_unique | 返回True,如果索引没有重复的值 |
unique | 计算索引的唯一值数组 |
转载于:https://www.cnblogs.com/sunshinewang/p/6924443.html
pandas之数据结构相关推荐
- 数据分析工具Pandas(1):Pandas的数据结构
Pandas Pandas的名称来自于面板数据(panel data)和Python数据分析(data analysis). Pandas是一个强大的分析结构化数据的工具集,基于NumPy构建,提供了 ...
- Pandas简明教程:二、Pandas基本数据结构-DataFrame与Series
文章目录 1.什么是表格? 2.DataFrame类型 3.Series类型 本系列教程教程完整目录: 1.什么是表格? 月份 姓名 性别 应发工资 实发工资 职位 0 1 张三 男 2000 150 ...
- python怎么分析数据结构_《利用Python进行数据分析》第五章-pandas的数据结构介绍...
pandas的数据结构介绍 要使用pandas,你首先就得熟悉它的两个主要数据结构:Series和DataFrame.虽然它们并不能解决所有问题,但它们为大多数应用提供了一种可靠的.易于使用的基础. ...
- 图解Pandas,数据结构介绍 | 图文第1篇
01写在前面 大家好,我是阳哥,欢迎来到「Python数据之道」. 本次是<图解Pandas>系列图文内容的 第 1 篇,主要是对Pandas中数据结构进行介绍. 本文是付费阅读文章,可免 ...
- Pandas之:深入理解Pandas的数据结构
文章目录 简介 Series 从**ndarray**创建 从dict创建 从标量创建 Series 和 ndarray Series和dict 矢量化操作和标签对齐 Name属性 **DataFra ...
- Pandas的数据结构之DataFrame
DataFrame的简介 DataFrame是一个[表格型]的数据结构,可以看做是[由Series组成的字典](共用一个索引).DataFrame由按一定顺序排列的多列数据组成.设计初衷是将Serie ...
- 【Pandas】数据结构Series 基本用法总结
对Series数据结构的理解: (1)Series是Pandas中的数据结构: (2)Series是DataFrame的一列数据: (3)Series的索引,默认为0,1,2,3,4,5...,与列表 ...
- python:Pandas的数据结构:Series,DataFrame
本篇文章主要讲一讲python的pandas库的一些使用(也算自己学习当中的整理): 数据分析的搭配环境,大家可以去参照安装下载好:https://blog.csdn.net/m0_53088614/ ...
- Python pandas 核心数据结构
一.用字典表创建Series数据一维 d = {'a': 0, 'b': 1, 'd':3} s = pd.Series(d, index=list('abcd')) 特性: 可以进行索引 print ...
最新文章
- eclipse中java项目转换为web项目
- 「AI 质检员」在富士通上岗,效率比人工高 25%
- XML 数据 BCP方式大批量的上传--学习(一)
- mysql用户名长度_如何增加PhpMyAdmin / mysql用户帐户的用户名长度?
- 强化学习(二)—— 价值学习(Value-Based)及DQN
- android 观察者更新ui,android通过观察者模式实现更新UI
- SpringBoot 集成 MyBatisPlus 模板
- C#统计字符出现的个数【C#】
- object detection错误之no module named nets
- 关于go访问rabbitmq的连接池
- byte与或运算 java_java中byte转换int时为何与0xff进行与运算
- c#开源消息队列中间件EQueue 教程
- 如何去各型MCU的官网上下载正确的数据手册
- IE 8 Beta 2中文版首份试用体验
- python 视频分段_Python 视频文件的分割和合并
- 新手入门学Python一定要知道的编程开发工具
- C/C++计算字符串的长度
- 正则表达式数字、字母、中文、符号
- CS231n课程笔记:神经网络笔记 2
- 印度网站前50名都是做什么的
热门文章
- 如何找到SAP Cloud for Customer标准培训和认证方面的信息
- 第十二届蓝桥杯JavaB组省赛H题 杨辉三角形
- php监听网页日志,如何用php程序监听一个不断增长的日志文件
- IntelliJ IDEA搭建一个web项目含配置tomcat
- php通知websocket,php实现websocket实时消息推送
- 删除排序链表中的重复元素Python解法
- filter执行先后问题_filter的执行顺序是怎样的?
- java整数四则运算课设_用面向对象方法设计实现整数的四则运算(java)
- 脚本安装mysql数据库_Linux使用脚本安装MySQL数据库
- 高压电是以交流,还是直流方式输送?