深入pandas 数据处理

三个阶段

  • 数据准备
  • 数据转化
  • 数据聚合

数据准备

  • 加载
  • 组装
    • 合并 - pandas.merge()
    • 拼接 - pandas.concat()
    • 组合 - pandas.DataFrame.combine_first()
  • 变形
  • 删除

合并

example1:

import numpy as np
import pandas as pd
frame1 = pd.DataFrame({'id':['ball','pencil','pen','mug','ashtray'],'price':[12.33,11.44,33.21,13.23,33.62]})
frame2 = pd.DataFrame({'id':['pencil','ball','pencil','pen'],'color':['white','red','red','black']})
pd.merge(frame1,frame2)

有必要定义合并操作的标准 用 on 来指定

example2:

frame2.columns=['brand2','id2']

pd.merge(frame1,frame2,on='brand') # 需要重新明明

pd.merge(frame1,frame2,right_on='brand', left_on='sid')

拼接

concatenation

numpy 的 concatenate()函数就是做这种拼接操作

array1=np.arange(9).reshape((3,3))
array2=np.arange(9).reshape((3,3))+6
np=concatenate([array1,array2],axis=1)# axis=1 从行拼接 axis=0 从列拼接

pandas的concat()函数可以做拼接操作

ser1=pd.concat([ser1,ser2])
# axis=1 从行拼接 axis=0 从列拼接
# join='inner' or 'outer'

组合

Series对象: combine_first()

组合的同时还可以对齐数据

ser1=pd.Series(np.random.rand(5),index=[1,2,3,4,5])
ser2=pd.Series(np.random.rand(4),index=[2,4,5,6])
ser1.combine_first(ser2)

轴向旋转

意思是 需要按照行重新调整列或者反过来

两个操作:

  • stacking 入栈, 把列转化为行
  • unstacking 出站, 把行转化为列
frame1=pd.DataFrame(np.arange(9).reshape(3,3),index=['w','b','r'], columns=['ball','pen','pencil'])
frame1.stack() # 得到一个Series对象
ser.unstack() # 得到一个DataFrame对象 # 长格式向宽格式转化: DateFrame.pivot
wideframe=longframe.pivot('color','item')

删除

  • 删除一列

del frame['ball']

  • 删除多余的行

frame.drop('white')

数据转化

删除重复数据

DataFrame 中duplicated()函数可以用来检测重复的行,返回bool型Series对象

dframe.duplicated()
# 得到过滤结果
dframe[dframe.duplicated()]
# 讲重复的行删除
dframe.drop_duplicates<>

映射

dict 映射关系比较好

replace() 替换元素
map() 新建一列
rename() 替换索引 ### 替换
newcolor={'rosso':'red','verde':'green'}
frame.replace(newcolors) ser.replace(np.nan, 0) ### 添加元素
price={'ball':5.56,'mug':4.3}
frame['price']=frame['item'].map(price) ### 重命名轴索引
reindex={o:'first',2:'second'}
frame.replace(reindex)
frame.replace(index={1:'first'}, columns={'item':'object'})
# inplace 参数: 是否改变调用函数对象本身

离散化

result=[12,34,67,55,28,90.99,12,3,56,74,44,87,23,49,89,87]
bins=[0,25,50,75,100]
# 对result用cut函数
cat=pd.cut(result,bins)
cat >>> type(cat)
<class 'pandas.core.categorical.Categorical'>
# 返回的是类别对象
cat.levels
cat.labels
# 类别中计数
pd.value_counts(cat)
# cut 函数中的labels标签 labels=['a','b','c']

异常值的检测和过滤

randframe=pd.DataFrame(np.random.randn(1000,3))

descibe()函数查看每一列的描述性统计量

假设讲比标准差大三倍的元素是为异常值,用std()函数可以求出每一列的标准差

randframe.std()

对DataFrame对象进行过滤

randframe[(np.abs(randframe)>(3*randframe.std())).any(1)]

排序

nframe=pd.DataFrame(np.arange(25).reshape(5,5))
# permutation(5)创建一个随机顺序整数
new_order=np.random.permutation(5) # 0-4
nframe.take(new_order)

随机取样

np.random.randint()函数
sample=np.random.randint(0,len(nframe),size=3)

字符串处理

内置字符串处理方法

split() 函数 切割

test='12312,bob'
test.split(',')
# ['12312', 'bob']

strip()函数 去空白

tokens=[s.strip() for s in test.split(',')]

join() 拼接

>>> strings=['1','2','3','45','5']
','.join(strings)

in index() find() 查找操作

test.index('bottom')
test.find('bottom')
'bottom' in test

count() 出现次数

test.count('bottom')

replace()

test.replace('A','a')

正则表达式

import re

几个类别:

  • 模式匹配
  • 替换
  • 切分

re.split()

text="This is        an \t odd \n text!"
re.split('\s+',text) # 内部过程
regex=re.compile('\s+')
regex.split(text)

re.findall()

# 以A开头不区分大小写
text='A! This is my address: 16 Boltom Avenue, Boston'
re.findall('[A,a]\w+',text)

数据聚合

GroupBy

SPLIT-APPLY-COMBINE 三个阶段

  • 分组
  • 用函数处理
  • 合并
# 实际上只使用了GroupBy函数
frame=pd.DataFrame({'color':['white','red','green','red','green'],'obj':['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.3,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]}) >>> frame
color obj price1 price2
0 white pen 5.56 4.75
1 red pencil 4.20 4.12
2 green pencil 1.30 1.60
3 red ashtray 0.56 0.75
4 green pen 2.75 3.15 # 想要根据color组,计算price1的均值
group=frame['price1'].groupby(frame['color'])
# 得到一个group对象
group.groups # 查看分组情况
group.mean() # 查看均值
group.sum() # 查看分组总和

等级分组

ggroup=frame['price1'].groupby([frame['color'],frame['obj']])

frame[['price1','price2']].groupby(frame['color']).mean()

组迭代

for name, group in frame.groupby('color'):
print(name)
print(group)

分组函数

group=frame.groupby('color')
group['price1'].quantile(0.6) # 直接计算分位数 # 自定义聚合函数
def range(series):
return series.max()-series.min()
group['price1'].agg(range) group.agg(range)

Pandas 高级应用 数据分析的更多相关文章

  1. 数据分析06 /pandas高级操作相关案例:人口案例分析、2012美国大选献金项目数据分析

    数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 目录 数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 1. ...

  2. pandas小记:pandas高级功能

    http://blog.csdn.net/pipisorry/article/details/53486777 pandas高级功能:面板数据.字符串方法.分类.可视化. 面板数据 {pandas数据 ...

  3. Pandas高级教程之:GroupBy用法

    Pandas高级教程之:GroupBy用法 目录 简介 分割数据 多index get_group dropna groups属性 index的层级 group的遍历 聚合操作 通用聚合方法 同时使用 ...

  4. pandas高级操作

    pandas高级操作 import numpy as np import pandas as pd from pandas import DataFrame,Series 替换操作 替换操作可以同步作 ...

  5. Pandas之:Pandas高级教程以铁达尼号真实数据为例

    Pandas之:Pandas高级教程以铁达尼号真实数据为例 目录 简介 读写文件 DF的选择 选择列数据 选择行数据 同时选择行和列 使用plots作图 使用现有的列创建新的列 进行统计 DF重组 简 ...

  6. pandas模块(数据分析)------Series

    pandas是一个强大的Python数据分析的工具包. pandas是基于NumPy构建的. pandas的主要功能: 具备对其功能的数据结构DataFrame.Series 集成时间序列功能 提供丰 ...

  7. 5,pandas高级数据处理

    1.删除重复元素 使用duplicated()函数检测重复的行,返回元素为布尔类型的Series对象,每个元素对应一行,如果该行不是第一次出现,则元素为True - keep参数:指定保留哪一重复的行 ...

  8. tushare+pandas实现财经数据分析

    写在前面的话: 这是一个优秀的财经接口包,博主平时工作中也有使用,觉得很好,现在分享一些使用心得给需要的人,tushare并不是一个炒股软件,只是一个提供pandas数据的工具,具体如何使用,因人而异 ...

  9. Pandas高级教程之:Dataframe的合并

    目录 简介 使用concat 使用append 使用merge 使用join 覆盖数据 简介 Pandas提供了很多合并Series和Dataframe的强大的功能,通过这些功能可以方便的进行数据分析 ...

随机推荐

  1. Storm-源码分析- metric

    首先定义一系列metric相关的interface, IMetric, IReducer, ICombiner (backtype.storm.metric.api) 在task中, 创建一系列bui ...

  2. 素数筛法—时间复杂度O(n)

    请你想出一个算法求出n以内(含n)的所有素数,要求算法的时间复杂度越小越好. 这里介绍一种算法——快速线性素数筛法(欧拉筛法),时间复杂度O(n). 诀窍在于:筛除合数时,保证每个合数只会被它的最小质 ...

  3. IPython的基本功能(转)

    原文:http://kochiya.me/www/posts/Ipython!.html 前几天偶然在公司内网上拖了一本 Learning IPython for Interactive Comput ...

  4. linux下的时间管理概述

    2017/6/21 时间这一概念在生活中至关重要,而在操作系统中也同样重要,其在系统中的功能绝不仅仅是给用户提供时间这么简单,内核的许多机制都依赖于时间子系统.但凡是要在某个精确的时间执行某个事件,必 ...

  5. 机器学习第5周--炼数成金-----决策树,组合提升算法,bagging和adaboost,随机森林。

    决策树decision tree 什么是决策树输入:学习集输出:分类觃则(决策树) 决策树算法概述 70年代后期至80年代初期,Quinlan开发了ID3算法(迭代的二分器)Quinlan改迚了ID3 ...

  6. Angular 笔记系列(一)项目组织与命名规范

    其实使用 Angular.js 做项目已经很久了,也遇到过许多问题.其中很多问题的出现都是因为没有按照规范或者最佳实践来做,大部分原因是学的不够细,很多 tips 没 get 到,用到项目中就会出现各 ...

  7. The Cheap KD 10 is my best shoe yet

    10 years of anything is fairly huge Cheap KD 10, but adding something as great as Flyknit causes it ...

  8. mac 安装Sequel Pro

    安装命令如下 Install the App Press Command+Space and type Terminal and press enter/return key. Run in Term ...

  9. pc端弹框

    pc弹框插件:http://aui.github.io/artDialog/doc/index.html#quickref-bubble PC端,手机端,layui很好用,分页啥的:http://ww ...

  10. hbase(二)

    一.HBase简介 1.1简介 hbase是bigtable的开源山寨版本.是建立的hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它介于nosql和RDBMS之间,仅能通过 ...