python数据分析:pandas数据结构与操作
来源:互联网 发布:贵阳大数据广场在哪里 编辑:程序博客网 时间:2024/05/21 17:14
pandas有两个常用的数据结构模块
Series和DataFrame
我们将对这两个数据结构模块进行学习
Series有点类似于一位数组的对象,也有点类似于字典,由一组数据以及一组与之相关的索引组成
我们可以简单的输入一些数据生成一个Series对象
In [1]: from pandas import *In [2]: test=Series([1,2,3,4])In [3]: testOut[3]: 0 11 22 33 4dtype: int64
当然索引不一定得是整数,我们也可以在生成Series的时候对其添加索引
In [4]: test2=Series([1,2,3,4],index=['a','b','c','d'])In [5]: test2Out[5]: a 1b 2c 3d 4dtype: int64In [6]: test2['a']Out[6]: 1In [7]: test2.indexOut[7]: Index(['a', 'b', 'c', 'd'], dtype='object')
索引的方式也是与字典类似,我们之前Numpy的数组中,所学习的一些方法在Series对象中同样适用
In [4]: test2=Series([1,2,3,4],index=['a','b','c','d'])In [5]: test2Out[5]: a 1b 2c 3d 4dtype: int64In [6]: test2['a']Out[6]: 1In [7]: test2.indexOut[7]: Index(['a', 'b', 'c', 'd'], dtype='object')
因为映射方式的类似,其实Python的字典可以直接利用里面的数据来创建一个Series
In [12]: height={'tom':170,'david':175,'harry':180,'mary':170}In [13]: test3=Series(height)In [14]: test3Out[14]: david 175harry 180mary 170tom 170dtype: int64
我们可以直接通过一个列表来传入索引
In [16]: name=['tom','david','harry','jack']In [17]: test4=Series(height,index=name)In [18]: test4Out[18]: tom 170.0david 175.0harry 180.0jack NaNdtype: float64
对于缺失的数值可以用pandas中的isnull和notnull函数检测
In [19]: pd.isnull(test4)Out[19]: tom Falsedavid Falseharry Falsejack Truedtype: boolIn [20]: pd.notnull(test4)Out[20]: tom Truedavid Trueharry Truejack Falsedtype: bool
Series对象本身和它的索引都有一个name属性,这有点像我们平时常用的 excel表格
In [21]: test3.name='height'In [22]: test3.index.name='Name'In [23]: test3Out[23]: Namedavid 175harry 180mary 170tom 170Name: height, dtype: int64
索引也可以通过赋值的方式修改
In [24]: test3.index=['bob','bob','bob','bob']In [25]: test3Out[25]: bob 175bob 180bob 170bob 170Name: height, dtype: int64
Dataframe是pandas里面类似表格型的一个数据结构,他有一组有序的列,每列可以是不同的值类型,又有点像是一个扩充的Series,是以二维结构存储数据。
构建Dataframe有很多办法,我们可以在一个字典的基础上来创建他
In [3]: data={'name':['tom','harry','jack','mary'],'height':['176','170','180','160'],'weight':['100','80','90','50']}In [4]: frame=DataFrame(data)In [5]: frameOut[5]: height name weight0 176 tom 1001 170 harry 802 180 jack 903 160 mary 50
Dataframe也可以按照指定序列进行排序
In [7]: DataFrame(data,columns=['name','height','weight'],index=['first','second','third','four'])Out[7]: name height weightfirst tom 176 100second harry 170 80third jack 180 90four mary 160 50
如果传入的数据在之中没有对应值也会像Series中一样生成NA值
指定了列序列,会按照指定的序列进行排序
In [7]: DataFrame(data,columns=['name','height','weight'],index=['first','second','third','four'])Out[7]: name height weightfirst tom 176 100second harry 170 80third jack 180 90four mary 160 50
可以通过索引把DataFrame的列获取为一个Series
In [9]: frame=DataFrame(data,columns=['name','height','weight'],index=['first','second','third','four'])In [10]: frame['name']Out[10]: first tomsecond harrythird jackfour maryName: name, dtype: object
加入索引字段ix可以获取一整行的信息
In [12]: frame.ix['third']Out[12]: name jackheight 180weight 90Name: third, dtype: object
还有一种常见的数据形式是嵌套字典,我们将其传入字典,会被解释为:外层字典的键作为主列,内层的键则作为行索引
In [13]: pop={'Nevada':{2001:2.4,2002:2.9},'ohio':{2000:1.5,2001:1.7,2002:3.6}}In [14]: frame=DataFrame(pop)In [15]: frameOut[15]: Nevada ohio2000 NaN 1.52001 2.4 1.72002 2.9 3.6
我们可以对其进行转置
In [16]: frame.TOut[16]: 2000 2001 2002Nevada NaN 2.4 2.9ohio 1.5 1.7 3.6
内层的字典会被合并,排序成为最后的列。如果指定过索引,则不会这样
In [18]: DataFrame(pop,index=[2001,2002,2003])Out[18]: Nevada ohio2001 2.4 1.72002 2.9 3.62003 NaN NaN
- python数据分析:pandas数据结构与操作
- Python 数据分析:pandas 操作基础篇
- Python数据分析库pandas基本操作
- python与数据分析 pandas 基础
- python数据分析---Pandas
- Python 数据分析 pandas
- Python数据分析入门(一)-Pandas数据结构(Series)
- [Python数据分析-01]Pandas数据结构之Series
- 【Python数据分析与展示】(四)pandas库基本操作
- python数据分析pandas包入门学习(一)pandas数据结构介绍
- python/pandas数据分析(十六)- 数据索引与选取
- 利用Python进行数据分析(15) pandas基础: 字符串操作
- 用python做数据分析|pandas库:DataFrame基本操作
- Python 数据分析-pandas 基础
- Python点滴(三)—pandas数据分析与matplotlib画图
- python/pandas/numpy数据分析(十一)-相关系数与协方差
- Python点滴(三)—pandas数据分析与matplotlib画图
- 数据分析之Pandas——数据结构
- opencv(10)---对比度亮度调整与通道分离与合并
- WHERE子句使用正则表达式进行搜索过滤
- 第八单元
- java中锁的深入理解(二)
- 数据结构——基础篇
- python数据分析:pandas数据结构与操作
- 浅谈嵌入式驱动设计
- lintcode(661)Convert BST to Greater Tree
- Python学习之 logging模块的使用
- 《程序员修炼之道》笔记(六)
- 第五次实验--数组—
- 学习AIDL遇到的问题
- 国外遥感影像如何下载之利用USGS网站影像下载无需JAVA
- 四、常用数据结构和函数