利用Python进行数据分析——pandas入门
利用Python进行数据分析——pandas入门
- 基于NumPy建立的
from pandas importSeries,DataFrame
,import pandas as pd
一、两种数据结构
1.Series###
类似于Python的字典,有索引和值
创建Series
#不指定索引,默认创建0-N
In [54]: obj = Series([1,2,3,4,5])
In [55]: obj
Out[55]:
0 1
1 2
2 3
3 4
4 5
dtype: int64
#指定索引
In [56]: obj1 = Series([1,2,3,4,5],index=['a','b','c','d','e'])
In [57]: obj1
Out[57]:
a 1
b 2
c 3
d 4
e 5
dtype: int64
#将Python中的字典转换为Series
In [63]: dic = {'a':1,'b':2,'c':3}
In [64]: obj2 = Series(dic)
In [65]: obj2
Out[65]:
a 1
b 2
c 3
dtype: int64
对Series进行数组运算(根据布尔型数组进行过滤、标量乘法、应用函数等)依旧会保留索引和值之间的对应关系。
对应index的值找不到就用NAN表示,且在算数运算中会自动补齐数据,不存在用NAN
2.DataFrame
DataFrame是一个表格型的数据结构,既有行索引也有列索引。
创建DataFrame
#传进去一个等长列表组成的字典
IIn [75]: data = {'name':['nadech','bob'],'age':[23,25],'sex':['male','female']}
In [76]: DataFrame(data)
Out[76]:
age name sex
0 23 nadech male
1 25 bob female
#指定列的顺序
In [77]: DataFrame(data,columns=['sex','name','age'])
Out[77]:
sex name age
0 male nadech 23
1 female bob 25
# 嵌套字典创建DataFrame
DataFrame的操作
#获取某一列
In [82]: frame['age'] /frame.age
Out[82]:
0 23
1 25
Name: age, dtype: int64
#赋值
In [86]: frame2
Out[86]:
age sex name grade
0 23 male nadech NaN
1 25 female bob NaN
In [87]: frame2['grade']=12
In [88]: frame2
Out[88]:
age sex name grade
0 23 male nadech 12
1 25 female bob 12
Index对象
In [14]: index = frame.index
In [15]: index
Out[15]: RangeIndex(start=0, stop=3, step=1)
# index 对象不可修改
In [16]: index[0]=3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
二、基本功能
1.Series和DataFrame的重新索引
#Series
In [25]: obj = Series(['nadech','aguilera','irenieee'],index=['a','b','c'])
In [26]: obj
Out[26]:
a nadech
b aguilera
c irenieee
dtype: object
In [27]: obj.reindex(['c','b','a'])
Out[27]:
c irenieee
b aguilera
a nadech
dtype: object
#####DataFrame
In [21]: frame
Out[21]:
one two three
a 0 1 2
b 3 4 5
c 6 7 8
#直接传进去的列表是对行的重新索引
In [22]: frame.reindex(['c','b','a'])
Out[22]:
one two three
c 6 7 8
b 3 4 5
a 0 1 2
#对列的重新索引需要参数columns
In [24]: frame.reindex(columns=['three','two','one'])
Out[24]:
three two one
a 2 1 0
b 5 4 3
c 8 7 6
2.删除指定轴上的项
#Series
In [28]: obj.drop('c')
Out[28]:
a nadech
b aguilera
dtype: object
In [30]: obj.drop(['b','a'])
Out[30]:
c irenieee
dtype: object
#####DataFrame
frame删除行索引直接删除,列索引删除需要指定axis=1
In [39]: frame
Out[39]:
one two three
a 0 1 2
b 3 4 5
c 6 7 8
In [40]: frame.drop('a')
Out[40]:
one two three
b 3 4 5
c 6 7 8
In [41]: frame.drop('one',axis=1)
Out[41]:
two three
a 1 2
b 4 5
c 7 8
3.索引、选取和过滤
Series索引
In [8]: obj
Out[8]:
a 0
b 1
c 2
d 3
dtype: int32
In [9]: obj['a']
Out[9]: 0
In [10]: obj[0]
Out[10]: 0
#注意利用标签切片和index 0-N是不同的
In [11]: obj[2:3]
Out[11]:
c 2
dtype: int32
In [12]: obj['c':'d']
Out[12]:
c 2
d 3
dtype: int32
DataFrame索引
#索取frame的列
In [24]: frame
Out[24]:
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
In [25]: frame['one']
Out[25]:
a 0
b 4
c 8
d 12
Name: one, dtype: int32
In [26]: frame[['one','two']]
Out[26]:
one two
a 0 1
b 4 5
c 8 9
d 12 13
#索取frame的行,标签索引
In [33]: frame.ix['a']
Out[33]:
one 0
two 1
three 2
four 3
Name: a, dtype: int32
In [31]: frame.ix[['a','b']]
Out[31]:
one two three four
a 0 1 2 3
b 4 5 6 7
#同时选取行和列
In [35]: frame.ix[['a','b'],['one','two']]
Out[35]:
one two
a 0 1
b 4 5
4.算数运算和数据对齐
#当存在不同的索引对计算时,会产生并集,和NAN,通过fill_value 可以传入参数
- add()
- sub()
- div()
- mul()
5.Series和DataFrame的运算
#series的索引会匹配到dataframe的列,然后向下广播
In [46]: frame
Out[46]:
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
In [47]: obj = frame.ix['a']
In [48]: obj
Out[48]:
one 0
two 1
three 2
four 3
Name: a, dtype: int32
In [49]: frame - obj
Out[49]:
one two three four
a 0 0 0 0
b 4 4 4 4
c 8 8 8 8
d 12 12 12 12
#可以指定series匹配到dataframe的列(即index)然后向右广播,即沿着列广播
In [51]: frame
Out[51]:
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
In [52]: obj2 = Series(np.arange(4),index=['a','b','c','d'])
In [53]: obj2
Out[53]:
a 0
b 1
c 2
d 3
dtype: int32
In [54]: frame.sub(obj2,axis=0) #dataframe的行用0、列用1
Out[54]:
one two three four
a 0 1 2 3
b 3 4 5 6
c 6 7 8 9
d 9 10 11 12
5.排序
#按轴上的索引排序
#Series
In [6]: obj
Out[6]:
a 0
c 1
b 2
d 3
In [8]: obj.sort_index()
Out[8]:
a 0
b 2
c 1
d 3
dtype: int32
#DataFrame
frame.sort_index()
frame.sort_index(axis=1)
6.obj.index.is_unique
可以用来判断index是否唯一
三、汇总和计算描述统计
描述和汇总统计
- count 非Na值的数量
- describe 针对Series或各DataFrame列计算汇总统计
- min/max 最下最大值 都是每列中的最值
- aigmin/argmax 最小、大值的索引位置
- idxmin/idxmax 能获取到最小值和最大值的索引值
- quantile 计算样本的分位数
- sum() 计算每列的和
- mean()计算每列的均值
- median 计算每列的算数中位数
- mad() 根据平均值计算平均绝对离差
- var 计算每列的方差
- std 计算每列的标准差
- skew 样本值的偏度(三阶矩)
- kurt 样本值的峰度(四阶矩)
- cumsum 样本值的累计和
- cummin/cummax 累计最大值和累计最小值
- cumprod 累计积
- diff 计算一阶差分
- pct_change 计算百分数变化
Series的唯一值、值的count数、
- obj.unique() 返回唯一值数组
- obj.value_counts() 计算各个值出现的次数
- pd.value_counts(obj.values) 这个也可以用来计算count数,是顶层的方法
- isin([]) 判断Series各个值是否包含于传入的值序列中
四、处理缺失数据
NAN处理方法
- dropna 删除空值
- fillna 给空值赋值
- isnull 判断是否有空值存在
- notnull
DataFrame.drop()复杂情况
In [49]: fram1
Out[49]:
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 NaN 6.5 3.0
In [50]: cleaned = fram1.dropna()
In [51]: cleaned
Out[51]:
0 1 2
0 1.0 6.5 3.0
In [52]: fram1.dropna(how='all')
Out[52]:
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
3 NaN 6.5 3.0
#如上形式丢弃列的空值,传入axis=1
填充缺失值
obj.fillna()暴力填充
fram.fillna({1:0.1,2:0.2}) 对dataframe可以指定列填充对应的缺失值
#传入method,可以给每列填充一个上一个非空的数字,并且可以通过limit限制每列填充的个数
implace =True 会产生新的对象
In [57]: df
Out[57]:
0 1 2
0 -0.018286 0.246567 1.115108
1 0.722105 0.984472 -1.709935
2 1.477394 NaN 1.362234
3 0.077912 NaN 0.414627
4 0.530048 NaN NaN
5 0.294424 NaN NaN
In [58]: df.fillna(method='ffill')
Out[58]:
0 1 2
0 -0.018286 0.246567 1.115108
1 0.722105 0.984472 -1.709935
2 1.477394 0.984472 1.362234
3 0.077912 0.984472 0.414627
4 0.530048 0.984472 0.414627
5 0.294424 0.984472 0.414627
In [59]: df.fillna(method='ffill',limit=2)
Out[59]:
0 1 2
0 -0.018286 0.246567 1.115108
1 0.722105 0.984472 -1.709935
2 1.477394 0.984472 1.362234
3 0.077912 0.984472 0.414627
4 0.530048 NaN 0.414627
5 0.294424 NaN 0.414627
五、层次化索引
DataFrame和层次化索引可以互相转换
frame.stack() /unstack()
利用Python进行数据分析——pandas入门的更多相关文章
- 利用python进行数据分析--pandas入门2
随书练习,第五章 pandas入门2 # coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd ...
- 利用python进行数据分析--pandas入门1
随书练习,第五章 pandas入门1 # coding: utf-8 # In[1]: from pandas import Series, DataFrame # In[2]: import pa ...
- 利用Python进行数据分析-Pandas(第一部分)
利用Python进行数据分析-Pandas: 在Pandas库中最重要的两个数据类型,分别是Series和DataFrame.如下的内容主要围绕这两个方面展开叙述! 在进行数据分析时,我们知道有两个基 ...
- 利用Python进行数据分析-Pandas(第六部分-数据聚合与分组运算)
对数据集进行分组并对各组应用一个函数(无论是聚合还是转换),通常是数据分析工作中的重要环节.在将数据集加载.融合.准备好之后,通常是计算分组统计或生成透视表.pandas提供了一个灵活高效的group ...
- 利用Python进行数据分析-Pandas(第四部分-数据清洗和准备)
在数据分析和建模的过程中,相当多的时间要用在数据准备上:加载.清理.转换以及重塑上.这些工作会占到分析时间的80%或更多.有时,存储在文件和数据库中的数据的格式不适合某个特定的任务.研究者都选择使用编 ...
- 利用Python进行数据分析-Pandas(第七部分-时间序列)
时间序列(time series)数据是一种重要的结构化数据形式,应用于多个领域,包括金融学.经济学.生态学.神经科学.物理学等.时间序列数据的意义取决于具体的应用场景,主要有以下几种: 时间戳(ti ...
- 利用Python进行数据分析-Pandas(第五部分-数据规整:聚合、合并和重塑)
在许多应用中,数据可能分散在许多文件或数据库中,存储的形式也不利于分析.本部分关注可以聚合.合并.重塑数据的方法. 1.层次化索引 层次化索引(hierarchical indexing)是panda ...
- 利用Python进行数据分析-Pandas(第三部分)
访问数据是使用本书所介绍的这些工具的第一步.这里会着重介绍pandas的数据输入与输出,虽然别的库中也有不少以此为目的的工具. 输入输出通常可以划分为几个大类:读取文本文件和其他更高效的磁盘存储格式, ...
- 利用Python进行数据分析-Pandas(第二部分)
上一个章节中我们主要是介绍了pandas两种数据类型的具体属性,这个章节主要介绍操作Series和DataFrame中的数据的基本手段. 一.基本功能 1.重新索引 pandas对象的一个重要方法是r ...
随机推荐
- tar 压缩和解压缩使用笔记
tar 压缩和解压缩使用笔记 1 文件 1.1 打包 1.1 压缩 $ tar czf myfile.txt.tar.gz ./myfile.txt 1.2 解压缩 解压缩到目录: $ mkdir o ...
- outlook邮箱邮件与企业邮箱同步(outlook本地文件夹邮件,web邮箱里没有)
用惯了outlook2010, 问题:今天将邮件放到自定义文件夹后,发现在web邮箱中看不到邮件了.不能同步到企业邮箱. 解决忙了一天,才知道是账户类型问题,pop3类型,只下载不上传.所以outlo ...
- 小强的HTML5移动开发之路(14)——Video标签详解
来自:http://blog.csdn.net/dawanganban/article/details/18180605 在前面的小强的HTML5移动开发之路(5)--制作一个漂亮的视频播放器中制作了 ...
- 基于Bootstrap的Metro风格模板
这几天在看Bootstrap的一些书,这里整理一下书中的一些模板,方便以后使用. 1.BootMetro http://www.guoxiaoming.com/bootmetro/ 2.Bootswa ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
- 对嵌入式开发C语言结构体的一点总结
今天冬至居然不上班,公司的良心啊!这回有心情写博客和日志了,好了,废话不多说.直接看下文: 鉴于嵌入式开发过程中,C语言结构体的使用当然是必不可少.话说,基础什么的比你会更牛逼的算法更重要,基础不牢, ...
- 用javah 导出类的头文件, 常见的错误及正确的使用方法
******************************************************************************** 用javah 导出类的头文件, 常见的 ...
- ORM对象关系映射之GreenDAO源码解析
上一篇我们学习了GreenDAO的CRUD基本操作,可以说是非常的方便的,而且GreenDAO的效率和性能远远高于其它两款流行的ORM框架,下面是我从官网找的一副它们三个ORM框架之间的性能测试的直观 ...
- AngularJS进阶(二十一)Angularjs中scope与rootscope区别及联系
Angularjs中scope与rootscope区别及联系 scope是html和单个controller之间的桥梁,数据绑定就靠他了.rootscope是各个controller中scope的桥梁 ...
- 03_Nginx添加新模块
1 进入nginx安装目录,查看nginx版本及其编译参数: [root@localhost nginx]# ./nginx -V nginx version: nginx/1.8.0 buil ...