pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental highlevel building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language. It is already well on its way toward this goal.
pandas 适用于一下数据:
pandas is well suited for many different kinds of data:
Tabular data with heterogeneously-typed columns, as in an SQL table or Excel spreadsheet
Ordered and unordered (not necessarily fixed-frequency) time series data.
Arbitrary matrix data (homogeneously typed or heterogeneous) with row and column labels
Any other form of observational / statistical data sets. The data actually need not be labeled at all to be placed into a pandas data structure
适用于表格数据,例如SQL或者Excel数据表;有序或者无序时间序列表;带有行索引和列索引的矩阵;用于数理统计和分析的数据。
三种数据结构
Series:序列,具有key-value特性,类似于一维数组
DataFrame:数据框,是一种二维数据结构,类似于ndarray,具有行索引和列索引
Panel :面板数据,是一种较为复杂的三维数据结构,类似于三维数组
第一步是导入本次练习所需要的模块(module)
Series
series=Series(...,index=[...]),键-值对应特性,指定数值和键值。
传入方法的数据可以使数组、ndarray对象、字典都可以。
索引可以使用属性index获得索引对象或者修改,对Series中的数据进行索引,包括整数索引,index索引。通过索引可对两个Series中的数据进行数学运算,其具有自动对齐功能。name属性和index.name属性可以操作对其进行命名。
DataFrame
frame=DataFrame(...,columns=[...],index=[...])
数据框提供行索引和列索引对元素进行索引,方法传入的数据可以使字典、二维数组等。
属性与Series类似。
索引对象
通过索引对象可以实现特定数值的获取,也可以判断索引值是否存在
重索引
重新排列索引或者增加索引,未定数值的为nan
重索引的参数,包括填充值,方法,限制,复制,层次
drop()数值的删除
索引,选择和过滤
整数索引、布尔索引、key索引
数学运算和数据对齐
函数应用和映射
使用广播的机制,对所有或者部分元素使用函数,获得一个新的对象
排序和获得等级
唯一化
对于索引不唯一,可以使用is_unique()判断是否唯一。可使用uniques()唯一化索引。
使重叠数据不重叠,或者按照某一或多个索引进行排序。
Panel
面板数据比较复杂一般较少使用,但是还是提供了很多方法。以下进行简单的演示:
陈浩杰
2017.5.22